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

}

}
相关推荐
一叶飘零_sweeeet4 分钟前
深入拆解 Java CAS:从底层原理到 ABA 问题实战
java·cas·并发编程
StackNoOverflow18 分钟前
Spring Security权限控制框架详解
java·数据库·sql
yaaakaaang23 分钟前
九、装饰器模式
java·装饰器模式
大熊背27 分钟前
如何利用Lv值实现三级降帧
算法·自动曝光·lv·isppipeline
d_dreamer28 分钟前
SeaTunnel推荐Maven版本
java·maven
清心歌39 分钟前
记一次系统环境变量更改后在IDEA中无法读取新值的排查过程
java·后端·intellij-idea·idea
大尚来也43 分钟前
驾驭并发:.NET多线程编程的挑战与破局之道
java·前端·算法
dong__csdn1 小时前
jdk添加信任证书
java·开发语言
向阳而生,一路生花1 小时前
深入浅出 JDK7 HashMap 源码分析
算法·哈希算法
hhcccchh1 小时前
1.1 HTML 语义化标签(header、nav、main、section、footer 等)
java·前端·html