sdut java lab6主观题

8-1 SDUT-JAVA-TestPassArray

分数 1

全屏浏览

作者 马新娟

单位 山东理工大学

This is an array, which name is a.Arayy a has 2 elements,a0 is 1 and a1 is 2.

Now please write two methods,one only passes by value and the other pass array.In these two methods,the elements of a are swapped.

You should write the JAVA program to display all the values before invoking the methods and after invoking the methods.

java 复制代码
public static void main(String\[\] args) {

    int\[\] a = {1, 2};

    System.out.println("调用方法之前:");

    displayArray(a);

    swapValue(a\[0\], a\[1\]);

    System.out.println("调用仅传递值的方法之后:");

    displayArray(a);

    swapArray(a);

    System.out.println("调用传递数组的方法之后:");

    displayArray(a);

}

public static void swapValue(int x, int y) {

    int temp = x;

    x = y;

    y = temp;

}

public static void swapArray(int\[\] arr) {

    int temp = arr\[0\];

    arr\[0\] = arr\[1\];

    arr\[1\] = temp;

}

public static void displayArray(int\[\] arr) {

    for (int i = 0; i < arr.length; i++) {

        System.out.print(arr\[i\] + " ");

    }

    System.out.println();

}

}
相关推荐
大熊背2 分钟前
ISP图像处理中大数乘法溢出处理(二)
算法·大数乘法
roman_日积跬步-终至千里16 分钟前
【算法3】二叉树中的最大路径和
算法
XUEYUAN521232 分钟前
IP 池调度算法实战:代理池负载均衡、健康检查与失效剔除机制(2026)
tcp/ip·算法·负载均衡
无小道33 分钟前
算法——问题转化
算法
evans在进步39 分钟前
LeetCode 53 最大子数组和:一次遍历掌握 Kadane 算法
算法·leetcode·职场和发展
山甫aa40 分钟前
日志技术 Logback + Slf4j —— 从零开始的 Web 后端学习
java·后端·学习·web·logback
韶博雅43 分钟前
开启补充日志
java·开发语言·sql
小七在进步1 小时前
数据结构:选择排序
数据结构·算法·排序算法
FYKJ_20101 小时前
django学习成绩预警系统10905
java·javascript·spring boot·python·spark·django·php
卓怡学长1 小时前
w192基于springboot教务管理系统
java·数据库·spring boot·spring·maven·intellij-idea