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

}

}
相关推荐
Hello.Reader21 小时前
Flink Java 版本兼容性与 JDK 模块化(Jigsaw)踩坑11 / 17 / 21 怎么选、怎么配、怎么稳
java·大数据·flink
AI科技星21 小时前
张祥前统一场论的数学表述与概念梳理:从几何公设到统一场方程
人工智能·线性代数·算法·机器学习·矩阵·数据挖掘
TechPioneer_lp21 小时前
小红书后端实习一面|1小时高强度技术追问实录
java·后端·面试·个人开发
程序员-King.21 小时前
day167—递归—二叉树的直径(LeetCode-543)
算法·leetcode·深度优先·递归
亲爱的非洲野猪21 小时前
2动态规划进阶:背包问题详解与实战
算法·动态规划·代理模式
TH_121 小时前
37、SQL的Explain
java·数据库·sql
YH12312359h21 小时前
战斗机目标检测与跟踪:YOLOv26算法详解与应用
算法·yolo·目标检测
康王有点困21 小时前
Flink部署模式
java·大数据·flink
EndingCoder21 小时前
属性和参数装饰器
java·linux·前端·ubuntu·typescript
芒克芒克21 小时前
LeetCode 134. 加油站(O(n)时间+O(1)空间最优解)
java·算法·leetcode·职场和发展