sdut java lab6主观题

8-1 SDUT-JAVA-TestPassArray

分数 1

全屏浏览

作者 马新娟

单位 山东理工大学

This is an array, which name is a.Arayy a has 2 elements,a0 is 1 and a1 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();

}

}
相关推荐
wuqingshun3141594 小时前
什么是责任链模式,一般用在什么场景?
java·责任链模式
:-)4 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
wuqingshun3141595 小时前
说一下消息队列的模型有哪些?
java
fīɡЙtīиɡ ℡6 小时前
布隆过滤器
java
yaoxin5211236 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
wuqingshun3141598 小时前
MYSQL的乐观锁和悲观锁是什么?
java
Jerry8 小时前
LeetCode 101. 对称二叉树
算法
唐青枫9 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java
jvmind_dev9 小时前
Java GC 实战指南(番外篇):被忽视的隐形杀手 —— Class Unloading 如何拖垮 GC
java·后端
可编程芯片开发9 小时前
基于MPPT最大功率跟踪的离网光伏发电系统Simulink建模与仿真
算法