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

}

}
相关推荐
pshdhx_albert1 小时前
AI agent实现打字机效果
java·http·ai编程
汀、人工智能2 小时前
[特殊字符] 第40课:二叉树最大深度
数据结构·算法·数据库架构·图论·bfs·二叉树最大深度
沉鱼.442 小时前
第十二届题目
java·前端·算法
赫瑞2 小时前
数据结构中的排列组合 —— Java实现
java·开发语言·数据结构
大熊背2 小时前
ISP Pipeline中Lv实现方式探究之三--lv计算定点实现
数据结构·算法·自动曝光·lv·isppipeline
西岸行者3 小时前
BF信号是如何多路合一的
算法
大熊背3 小时前
ISP Pipeline中Lv实现方式探究之一
算法·自动白平衡·自动曝光
周末也要写八哥3 小时前
多进程和多线程的特点和区别
java·开发语言·jvm
罗西的思考4 小时前
【OpenClaw】通过 Nanobot 源码学习架构---(5)Context
人工智能·算法·机器学习
惜茶4 小时前
vue+SpringBoot(前后端交互)
java·vue.js·spring boot