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

}

}
相关推荐
a587692 小时前
Elasticsearch核心概念与Java实战:从入门到精通
java·es
元亓亓亓2 小时前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
纪元A梦2 小时前
贪心算法在SDN流表优化中的应用
算法·贪心算法
JCBP_3 小时前
QT(4)
开发语言·汇编·c++·qt·算法
码熔burning3 小时前
JVM 垃圾收集算法详解!
jvm·算法
Brookty3 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
tellmewhoisi3 小时前
前置配置1:nacos 基本配置(注册与发现)
java
小柴狗3 小时前
C语言关键字详解:static、const、volatile
算法
会开花的二叉树3 小时前
继承与组合:C++面向对象的核心
java·开发语言·c++
长河5 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言