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

}

}
相关推荐
c***937713 分钟前
SpringBoot实现异步调用的方法
java·spring boot·spring
青衫码上行20 分钟前
【Java Web学习 | 第15篇】jQuery(万字长文警告)
java·开发语言·前端·学习·jquery
凯子坚持 c1 小时前
Docker 容器实战:从镜像管理到私有仓库构建深度解析
java·docker·eureka
q***01652 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
shangjian0074 小时前
AI大模型-评价指标-相关术语
人工智能·算法
f***68604 小时前
【SpringBoot篇】详解Bean的管理(获取bean,bean的作用域,第三方bean)
java·spring boot·后端
Live&&learn5 小时前
算法训练-数据结构
数据结构·算法·leetcode
likuolei5 小时前
Eclipse 快捷键
java·ide·eclipse
松岛雾奈.2306 小时前
机器学习--PCA降维算法
人工智能·算法·机器学习
电子_咸鱼6 小时前
【STL string 全解析:接口详解、测试实战与模拟实现】
开发语言·c++·vscode·python·算法·leetcode