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

}

}
相关推荐
Knight_AL9 分钟前
Tomcat 类加载器隔离机制的实际应用
java·tomcat
FreeBuf_12 分钟前
Spring两大漏洞可导致泄露敏感信息及安全防护绕过(CVE-2025-41253/41254)
java·安全·spring
江太翁21 分钟前
Kotlin 与 Java 互操作中常用注解
java·python·kotlin
知星小度S25 分钟前
算法训练之多源BFS
算法·宽度优先
notillusion25 分钟前
URR#69598
java·php·程序优化
2201_7588754428 分钟前
LeetCode:19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
notillusion42 分钟前
CUK#22162
java·php·程序优化
RainbowSea44 分钟前
11. Spring AI + ELT
java·spring·ai编程
RainbowSea1 小时前
12. 模型RAG评测
java·spring·ai编程
代码不停1 小时前
Java前缀和算法题目练习
java·开发语言·算法