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雨后彩虹1 分钟前
最大数字问题
java·数据结构·算法·华为·面试
java修仙传5 分钟前
力扣hot100:搜索二维矩阵
算法·leetcode·矩阵
梦里不知身是客116 分钟前
tomcat作用和功能以及默认端口号
java·tomcat
码界奇点11 分钟前
基于SpringBoot与Vue3的多租户中后台管理系统设计与实现
java·spring boot·后端·spring·车载系统·毕业设计·源代码管理
浅川.2513 分钟前
xtuoj 字符串计数
算法
长安城没有风14 分钟前
在 IntelliJ IDEA 中高效使用 Git 的实用指南
java·git·intellij-idea
Code blocks14 分钟前
SpringBoot从0-1集成Netty实现自定义协议开发
java·spring boot·后端
天`南14 分钟前
【群智能算法改进】一种改进的金豺优化算法IGJO[1](动态折射反向学习、黄金正弦策略、自适应能量因子)【Matlab代码#94】
学习·算法·matlab
武子康19 分钟前
Java-195 RabbitMQ BlockingQueue 手搓“消息中间件”雏形:生产者-消费者模型到企业级 MQ 差在哪
java·分布式·架构·消息队列·rabbitmq·java-rabbitmq·mq
Propeller20 分钟前
【Android】动态操作 Window 的背后机制
android·java