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();

}

}
相关推荐
Han.miracle1 分钟前
Java集合核心:ArrayList与LinkedList深度解析
java·开发语言
篱笆院的狗2 分钟前
Group by很慢,如何定位?如何优化?
java·数据库
期待のcode23 分钟前
Java的反射
java·开发语言
2201_7578308738 分钟前
AOP入门程序
java·开发语言
雨中飘荡的记忆39 分钟前
MyBatis反射模块详解
java·mybatis
宸津-代码粉碎机41 分钟前
Spring 6.0+Boot 3.0实战避坑全指南:5大类高频问题与解决方案(附代码示例)
java·数据仓库·hive·hadoop·python·技术文档编写
笃行客从不躺平43 分钟前
ThreadLocal 复习一
java·开发语言
程序帝国1 小时前
SpringBoot整合RediSearch(完整,详细,连接池版本)
java·spring boot·redis·后端·redisearch
安卓程序员_谢伟光1 小时前
如何监听System.exit(0)的调用栈
java·服务器·前端
Pluto_CSND1 小时前
JSONPath解析JSON数据结构
java·数据结构·json