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

}

}
相关推荐
IAUTOMOBILE几秒前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi几秒前
PostgreSQL版本选择
java
皮皮林5517 分钟前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea
MX_935920 分钟前
SpringMVC请求参数
java·后端·spring·servlet·apache
ID_1800790547326 分钟前
小红书笔记评论 API,Python 调用示例与完整 JSON 返回参考
java·开发语言
Fuxiao___29 分钟前
C 语言核心知识点讲义(循环 + 函数篇)
算法·c#
lifewange30 分钟前
java连接Mysql数据库
java·数据库·mysql
漫随流水1 小时前
c++编程:反转字符串(leetcode344)
数据结构·c++·算法
云原生指北1 小时前
命令行四件套:fd-rg-fzf-bat
java·大数据·elasticsearch
人间打气筒(Ada)2 小时前
go实战案例:如何通过 Service Meh 实现熔断和限流
java·开发语言·golang·web·istio·service mesh·熔断限流