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

}

}
相关推荐
炽烈小老头7 分钟前
【每天学习一点算法 2026/04/20】除自身以外数组的乘积
学习·算法
skilllite作者37 分钟前
AI agent 的 Assistant Auto LLM Routing 规划的思考
网络·人工智能·算法·rust·openclaw·agentskills
aq55356001 小时前
编程语言三巨头:汇编、C++与PHP大比拼
java·开发语言
我是无敌小恐龙1 小时前
Java SE 零基础入门Day01 超详细笔记(开发前言+环境搭建+基础语法)
java·开发语言·人工智能·opencv·spring·机器学习
心态与习惯2 小时前
Julia 初探,及与 C++,Java,Python 的比较
java·c++·python·julia·比较
py有趣2 小时前
力扣热门100题之不同路径
算法·leetcode
一叶飘零_sweeeet2 小时前
优秀文章合集
java
_日拱一卒3 小时前
LeetCode:25K个一组翻转链表
算法·leetcode·链表
zopple3 小时前
ThinkPHP5.x与3.x核心差异解析
java·python·php
啊哦呃咦唔鱼3 小时前
LeetCodehot100-394 字符串解码
算法