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

}

}
相关推荐
大模型丫丫8 分钟前
Spring Boot 入门指南:从零开始构建你的第一个应用
java·spring boot·后端
拾光Ծ15 分钟前
Linux五种I/O模型与fcntl非阻塞控制
java·linux·服务器·i/o模型
半旧51819 分钟前
【Java Web02】WEB服务器内部实现技术揭秘
java·服务器·前端
gis开发之家21 分钟前
日志怎么打才专业?Spring Boot 4 日志体系与 Logback 配置(生产级实战)
java·spring boot·后端·logback
2601_9507607923 分钟前
TNF-α:自身免疫疾病治疗的核心靶点与信号通路解析
人工智能·算法·蛋白
SaaS发言人28 分钟前
渠道数字化解决方案横评:谁更懂快消
java·大数据·人工智能
JL1528 分钟前
Java并发编程面试全攻略-从synchronized到AQS底层原理
java·开发语言·面试·并发编程
不会代码的小猴37 分钟前
2. 了解Qt
开发语言·c++·笔记·qt·算法
ZC跨境爬虫37 分钟前
LeetCode 119. 杨辉三角 II(原地更新优化详解 + Java Python 实现)
java·python·leetcode
比奇堡裤头村39 分钟前
统计学习方法——支持向量机
算法·支持向量机·学习方法