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

}

}
相关推荐
SteveSenna3 分钟前
Trossen Arm MuJoCo自定义1:改变目标物体
人工智能·学习·算法·机器人
yong999025 分钟前
IHAOAVOA:天鹰优化算法与非洲秃鹫优化算法的混合算法(Matlab实现)
开发语言·算法·matlab
Stella Blog36 分钟前
狂神Java基础学习笔记Day03
java·笔记·学习
米粒11 小时前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展
zopple2 小时前
四大编程语言对比:PHP、Python、Java与易语言
java·python·php
逍遥德2 小时前
Java 锁(线程间)和数据库锁(事务间)对比详解
java·数据库·sql·高并发·锁机制
gwjcloud2 小时前
Docker详解
java·docker·容器
河阿里2 小时前
Java-JWT令牌技术深度指南
java·开发语言
WiChP3 小时前
【V0.1B6】从零开始的2D游戏引擎开发之路
java·log4j·游戏引擎
leaves falling3 小时前
C/C++ 的内存管理,函数栈帧详讲
java·c语言·c++