sdut java lab6主观题

8-1 SDUT-JAVA-TestPassArray

分数 1

全屏浏览

作者 马新娟

单位 山东理工大学

This is an array, which name is a.Arayy a has 2 elements,a0 is 1 and a1 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();

}

}
相关推荐
波尔德几秒前
Origin 2024 绘制钟形正态分布曲线:填充颜色并导出透明 PNG
人工智能·算法
Q26433650233 分钟前
【有源码】基于SpringBoot+Vue的博物馆展览与服务一体化平台-java博物馆数字化展品展示与交互平台设计
java·vue.js·spring boot·spring·毕业设计·javaweb·课程设计
guo_wen_qiang8 分钟前
项目使用sentinel遇到的问题
java·sentinel
Omics Pro11 分钟前
微软:多模态生物世界模型
数据库·人工智能·算法·microsoft·机器学习·自然语言处理
CarIise13 分钟前
IDEA与Maven基础课堂笔记
java·intellij-idea·caffe
hai31524754317 分钟前
语言学集合定律
人工智能·算法
小溪学编程19 分钟前
C语言篇:枚举类型
java·c语言·前端
小溪学编程21 分钟前
C语言篇:语言结构
c语言·开发语言·算法
云和数据.ChenGuang28 分钟前
JAVAEE AI工程师路线图
java·人工智能·java-ee·fastapi·springai·langchain4j
2601_9628857228 分钟前
如何用 Python 统计 A 股连续涨停(几连板)?
java·linux·python