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

}

}
相关推荐
张文君3 分钟前
ubuntu26.04坏块分区隔离急速版
java·前端·python
positive_zpc6 分钟前
进阶数据结构图——最小生成树(一)
数据结构·算法·图论
旧梦952710 分钟前
Java 枚举类详解:从基础语法到高级用法
java·开发语言·python
码完就睡16 分钟前
数据结构——遍历二叉树
数据结构·算法
evans在进步19 分钟前
LeetCode 238 除自身以外数组的乘积:前缀积与后缀积详解
算法·leetcode·职场和发展
Brilliantwxx28 分钟前
【C++】初入嵌入式C++复习-----经典面试题100道
开发语言·c++·算法·面试·职场和发展
动词ing31 分钟前
【学习笔记】字符串(遍历+反转+替换+子串匹配+KMP)+题目解析
学习·算法
Freak嵌入式37 分钟前
告别死循环!Pico 外部中断控制 LED,比轮询高效 100 倍
java·开发语言·stm32·单片机·嵌入式硬件
大模型码小白41 分钟前
MCP协议开发实战:从零搭建AI Agent工具链
运维·人工智能·算法·机器学习·自动化
2601_9621229743 分钟前
解决 IntelliJ IDEA 中 Tomcat 日志乱码问题的详细指南
java·tomcat·intellij-idea