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

}

}
相关推荐
Дерек的学习记录2 小时前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
hzc09876543212 小时前
Spring Integration + MQTT
java·后端·spring
yugi9878382 小时前
无线传感器网络中GAF算法节点特性分析
网络·算法
1027lonikitave2 小时前
使用斐波那契数列讲解尾递归
算法
前路不黑暗@3 小时前
Java项目:Java脚手架项目的公共模块的实现(二)
java·开发语言·spring boot·学习·spring cloud·maven·idea
人道领域3 小时前
Spring核心注解全解析
java·开发语言·spring boot
金牌归来发现妻女流落街头4 小时前
日志级别是摆设吗?
java·spring boot·日志
滴滴答滴答答4 小时前
LeetCode Hot100 之 16 合并两个有序链表
算法·leetcode·链表
MOONICK4 小时前
C#基础入门
java·开发语言
ASKED_20194 小时前
企业级大模型微调(Fine-tuning)策略
大数据·人工智能·算法