sdut java lab6主观题

简介: 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.

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


目录
打赏
0
0
0
0
10
分享
相关文章
|
10月前
|
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
73 1
|
10月前
|
sdut java lab5
sdut java lab5
51 0
|
10月前
|
sdut java lab7.2(法二)
sdut java lab7.2(法二)
57 0
|
10月前
|
SDUT java lab7.4
SDUT java lab7.4
51 0
|
10月前
|
SDUT JAVA lab3.9
SDUT JAVA lab3.9
61 2
|
10月前
|
SDUT Java lab6
SDUT Java lab6
38 0
|
10月前
|
sdut java lab7单选
sdut java lab7单选
62 0
|
10月前
|
sdut java lab 7.6
sdut java lab 7.6
62 0
|
1月前
|
【Java并发】【线程池】带你从0-1入门线程池
欢迎来到我的技术博客!我是一名热爱编程的开发者,梦想是编写高端CRUD应用。2025年我正在沉淀中,博客更新速度加快,期待与你一起成长。 线程池是一种复用线程资源的机制,通过预先创建一定数量的线程并管理其生命周期,避免频繁创建/销毁线程带来的性能开销。它解决了线程创建成本高、资源耗尽风险、响应速度慢和任务执行缺乏管理等问题。
166 60
【Java并发】【线程池】带你从0-1入门线程池
Java网络编程,多线程,IO流综合小项目一一ChatBoxes
**项目介绍**:本项目实现了一个基于TCP协议的C/S架构控制台聊天室,支持局域网内多客户端同时聊天。用户需注册并登录,用户名唯一,密码格式为字母开头加纯数字。登录后可实时聊天,服务端负责验证用户信息并转发消息。 **项目亮点**: - **C/S架构**:客户端与服务端通过TCP连接通信。 - **多线程**:采用多线程处理多个客户端的并发请求,确保实时交互。 - **IO流**:使用BufferedReader和BufferedWriter进行数据传输,确保高效稳定的通信。 - **线程安全**:通过同步代码块和锁机制保证共享数据的安全性。
68 23