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

}

}
相关推荐
2501_9130613430 分钟前
网络原理知识
java·网络
希望永不加班37 分钟前
Spring AOP 代理模式:CGLIB 与 JDK 动态代理区别
java·开发语言·后端·spring·代理模式
flushmeteor1 小时前
java的动态代理和字节码生成技术
java·动态代理·代理·字节码生成
eggwyw1 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
0xDevNull1 小时前
MySQL 别名(Alias)指南:从入门到避坑
java·数据库·sql
被开发耽误的大厨1 小时前
1、==、equals、hashCode底层原理?重写场景?
算法·哈希算法
lv__pf2 小时前
springboot原理
java·spring boot·后端
java1234_小锋2 小时前
Java高频面试题:什么是可重入锁?
java·开发语言
WolfGang0073212 小时前
代码随想录算法训练营 Day38 | 动态规划 part11
算法·动态规划
云烟成雨TD2 小时前
Spring AI Alibaba 1.x 系列【22】Agent 并行工具执行与超时 / 协作式取消实战
java·人工智能·spring