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

}

}
相关推荐
j_xxx404_2 分钟前
常见位运算基础知识,技巧总结以及力扣实战
数据结构·c++·算法·leetcode
xxxxxxllllllshi4 分钟前
java值传递和引用传递的区别?举例一些常见都笔试面试题说明,最后有速记口诀
java·开发语言
RuiBo_Qiu7 分钟前
【LLM进阶-后训练&部署】1. 大语言模型全参数微调:从前向推理到反向传播的底层原理解析
人工智能·算法·语言模型·自然语言处理·ai-native
huabiangaozhi7 分钟前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
小王不爱笑1328 分钟前
Java List 集合全面解析:ArrayList、LinkedList 与 Vector 的深度对比
java·windows·list
子有内涵8 分钟前
【Linux】程序地址空间(是什么?为什么?)
linux·运维·算法
setmoon2149 分钟前
C++与量子计算模拟
开发语言·c++·算法
KIKIiiiiiiii13 分钟前
微信自动化机器人开发
java·开发语言·人工智能·python·微信·自动化
实心儿儿14 分钟前
算法6:相交链表
数据结构·算法·链表
victory043114 分钟前
containerd打包命令 和NFS挂载
java·开发语言