sdut java lab6主观题

8-1 SDUT-JAVA-TestPassArray

分数 1

全屏浏览

作者 马新娟

单位 山东理工大学

This is an array, which name is a.Arayy a has 2 elements,a[0] is 1 and a[1] 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();

}

}
相关推荐
Vincent_Vang2 分钟前
多态 、抽象类、抽象类和具体类的区别、抽象方法和具体方法的区别 以及 重载和重写的相同和不同之处
java·开发语言·前端·ide
qualifying3 分钟前
JavaEE——多线程(3)
java·开发语言·java-ee
努力学算法的蒟蒻7 分钟前
day49(12.30)——leetcode面试经典150
算法·leetcode·面试
天赐学c语言7 分钟前
12.30 - 合并区间 && C++中class和C语言中struct的区别
c语言·c++·算法·leecode
花卷HJ16 分钟前
Android 下载管理器封装实战:支持队列下载、取消、进度回调与自动保存相册
android·java
wanghowie18 分钟前
01.01 Spring核心|IoC容器深度解析
java·后端·spring
人道领域21 分钟前
【零基础学java】(Map集合)
java·开发语言
@淡 定21 分钟前
Seata AT模式详细实例:电商下单场景
java
杀死那个蝈坦22 分钟前
JUC并发编程day1
java·开发语言
有一个好名字24 分钟前
力扣-递增的三元子序列
算法·leetcode·职场和发展