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

}

}
相关推荐
Frostnova丶2 小时前
LeetCode 190.颠倒二进制位
java·算法·leetcode
骇城迷影2 小时前
代码随想录:链表篇
数据结构·算法·链表
闻哥2 小时前
Redis事务详解
java·数据库·spring boot·redis·缓存·面试
hrhcode2 小时前
【Netty】五.ByteBuf内存管理深度剖析
java·后端·spring·springboot·netty
道亦无名3 小时前
aiPbMgrSendAck
java·网络·数据库
专注前端30年3 小时前
智能物流路径规划系统:核心算法实战详解
算法
发现你走远了3 小时前
Windows 下手动安装java JDK 21 并配置环境变量(详细记录)
java·开发语言·windows
心 -3 小时前
java八股文DI
java
json{shen:"jing"}3 小时前
字符串中的第一个唯一字符
算法·leetcode·职场和发展
黎雁·泠崖4 小时前
Java常用类核心详解(一):Math 类超细讲解
java·开发语言