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

}

}
相关推荐
想学习java初学者几秒前
SpringBoot整合GS1编码解码
java·spring boot·后端
日月云棠1 分钟前
2 快速入门实战指南
java·后端
日月云棠2 分钟前
3 Dubbo 2.7 高级配置:检查控制、版本策略与协议选择
java·后端
叶小鸡7 分钟前
小鸡玩算法-力扣HOT100-动态规划(上)
算法·leetcode·动态规划
砍材农夫8 分钟前
物联网 基于netty构建mqtt协议规范(主题通配符订阅)
java·前端·javascript·物联网·netty
掉鱼的猫11 分钟前
用 Solon AI 从零构建 MCP 工具服务:让 AI Agent 拥有真实世界的能力
java·llm·mcp
日月云棠11 分钟前
1 分布式架构演进与Dubbo框架入门
java·后端
LuminousCPP13 分钟前
数据结构 - 线性表第三篇:基于顺序表实现 C 语言通讯录(基础功能篇)
c语言·数据结构·经验分享·笔记·算法
_日拱一卒18 分钟前
LeetCode:114二叉树展开为链表
java·开发语言·算法
SamDeepThinking23 分钟前
面试官问Bean线程安全,你该从架构角度回答
java·后端·面试