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

}

}
相关推荐
tb_first3 小时前
万字超详细苍穹外卖学习笔记3
java·jvm·笔记·学习·spring·tomcat·maven
weixin_445402303 小时前
模板元编程应用场景
开发语言·c++·算法
啊阿狸不会拉杆3 小时前
《机器学习导论》第 1 章 - 引言
人工智能·python·算法·机器学习·ai·numpy·matplotlib
轻轻唱3 小时前
2026专业PPT设计服务商推荐:TOP10深度评测与选择指南
大数据·人工智能·算法
s1hiyu3 小时前
嵌入式C++低功耗设计
开发语言·c++·算法
fanstuck4 小时前
从 0 到 1 构建企业智能体平台:openJiuwen 架构解析与智能客服工作流实战
大数据·人工智能·算法·架构·aigc
青桔柠薯片4 小时前
数据结构:单向链表和双向链表
数据结构·算法·链表
我待_JAVA_如初恋4 小时前
安装idea教程
java·ide·intellij-idea
2401_841495644 小时前
【LeetCode刷题】对称二叉树
数据结构·python·算法·leetcode·二叉树··递归
飞机和胖和黄4 小时前
王道考研C语言第五周
c语言·考研·算法