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

}

}
相关推荐
t***44231 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
java
daidaidaiyu1 小时前
SpringCloud 微服务实现一则 (Eureka + Hystrix)
java·spring
plus4s2 小时前
2月12日(70-72题)
算法
小哥不太逍遥2 小时前
Technical Report 2024
java·服务器·前端
m0_672703312 小时前
上机练习第24天
算法
edisao2 小时前
序幕-内部审计备忘录
java·jvm·算法
shehuiyuelaiyuehao3 小时前
22Java对象的比较
java·python·算法
常利兵3 小时前
吃透Java操作符高阶:位操作符+赋值操作符全解析(Java&C区别+实战技巧+面试考点)
java·c语言·面试
不用89k3 小时前
SpringBoot学习新手项初识请求
java·spring boot·学习
码农阿豪3 小时前
SpringBoot实现公正有趣好玩的年会抽奖系统
java·spring boot·后端