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

}

}
相关推荐
蓝斯4976 小时前
[原创]《C#高级GDI+实战:从零开发一个流程图》第章:增加贝塞尔曲线,上、下、左、右连接点
java·c#·流程图
前端工作日常6 小时前
我学习到的Java 的 Service 分层:itf 和 impl 到底是什么?
java·后端
都叫我大帅哥7 小时前
Spring @Transactional 注解完全指南
java·spring
Java成神之路-7 小时前
G1 垃圾回收器 :SATB + TAMS核心机制深度解析
java·jvm
前端工作日常7 小时前
我学习到的JIT即时编译与机器码缓存失效
java·后端
带刺的坐椅7 小时前
Solon 的 10 种 HTTP 服务器:改一行依赖,换一个引擎
java·solon·jetty·undertow·mcp-server·htttp
luj_17688 小时前
星火科技助力边远地区防病攻坚
c语言·开发语言·c++·经验分享·算法
xcLeigh8 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
AI大模型-小华9 小时前
Codex 三方充值快速入门指南
java·前端·数据库·chatgpt·ai编程·codex·chatgpt pro
Mark_ZP9 小时前
【锁1】Synchronized vs ReentrantLock区别
java