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

}

}
相关推荐
Warren9837 分钟前
如何在 Spring Boot 中安全读取账号密码等
java·开发语言·spring boot·后端·安全·面试·测试用例
杨杨杨大侠1 小时前
第1篇:走进日志框架的世界 - 从HelloWorld到企业级应用
java·开源·apache log4j
David爱编程2 小时前
Java 内存模型(JMM)全景图:并发世界的底层基石
java·后端
Ldawn_AI2 小时前
4+ 图论高级算法
算法·深度优先·图论
Xの哲學2 小时前
Linux PCI 子系统:工作原理与实现机制深度分析
linux·网络·算法·架构·边缘计算
努力努力再努力wz2 小时前
【c++进阶系列】:万字详解多态
java·linux·运维·开发语言·c++
秦亿凡2 小时前
多线程下为什么用ConcurrentHashMap而不是HashMap
java·开发语言
知其然亦知其所以然3 小时前
SpringAI + Groq 实战:3 分钟教你搭建超快聊天机器人!
java·后端·openai
阿波罗尼亚3 小时前
ExcelUtils实现 设置内容 插入行 复制行列格式
java·开发语言
Monkey-旭3 小时前
Android 定位技术全解析:从基础实现到精准优化
android·java·kotlin·地图·定位