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

}

}
相关推荐
pianmian11 小时前
类(JavaBean类)和对象
java
我叫小白菜1 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
森焱森1 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
Albert Edison2 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍2 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122462 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~3 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇3 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097623 小时前
JavaEE-Linux环境部署
java·linux·java-ee
QuantumStack3 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法