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

}

}
相关推荐
格鸰爱童话2 分钟前
向AI学习项目技能(五)
java·学习
程序员萌萌5 分钟前
Java之mysql实战讲解(三):聚簇索引与非聚簇索引
java·mysql·聚簇索引
tankeven11 分钟前
动态规划专题(03):区间动态规划从原理到实践(未完待续)
c++·算法·动态规划
好家伙VCC19 分钟前
**发散创新:基于Python与ROS的机器人运动控制实战解析**在现代机器人系统开发中,**运动控制**是实现智能行为的核心
java·开发语言·python·机器人
程途知微1 小时前
ConcurrentHashMap线程安全实现原理全解析
java·后端
Mars酱1 小时前
1分钟编写贪吃蛇 | JSnake贪吃蛇单机版
java·后端·开源
devpotato1 小时前
人工智能(四)- Function Calling 核心原理与实战
java·人工智能
田梓燊1 小时前
2026/4/11 leetcode 3741
数据结构·算法·leetcode
默 语1 小时前
Records、Sealed Classes这些新特性:Java真的变简单了吗?
java·开发语言·python
斯内科1 小时前
FFT快速傅里叶变换
算法·fft