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

}

}
相关推荐
夫礼者5 分钟前
【极简监控】综合实战篇:1+1>>10 的降维打击!联动底层工具,暴力提取 SkyWalking“断头链路”
java·监控
05候补工程师7 分钟前
【ROS 2 避坑指南】从 SLAM 实时建图到 Nav2 导航算法深度调优全过程
算法·ubuntu·机器人
Dlrb121110 分钟前
C语言-函数传参
c语言·数据结构·算法
洛水水7 小时前
【力扣100题】18.随机链表的复制
算法·leetcode·链表
南宫萧幕8 小时前
规则基 EMS 仿真实战:SOC 区间划分与 Simulink 闭环建模全解
算法·matlab·控制
多加点辣也没关系8 小时前
数据结构与算法|第二十三章:高级数据结构
数据结构·算法
庞轩px8 小时前
第七篇:Spring扩展点——如何优雅地介入Bean的创建流程
java·后端·spring·bean·aware·扩展点
tongluowan00710 小时前
一个请求在Spring MVC 中是怎么流转的
java·spring·mvc
hoiii18710 小时前
孤立森林 (Isolation Forest) 快速异常检测系统
算法
夜郎king10 小时前
Spring AI 对接大模型开发易错点总结与实战解决办法
java·人工智能·spring