当前位置:网站首页>DS220702-0707作业
DS220702-0707作业
2022-08-11 05:12:00 【余谦吖】
#1.已有数组保存1,3,5,2,4,6
控制台输出:6 4 2 5 3 1
public class Main {
public static void main(String[] args) {
int[] arr = {1,3,5,2,4,6};
for (int i = arr.length-1; i >=0 ; i--) {
System.out.print(arr[i]+" ");
}
}
}

#2、已有数组保存1,5,9,3,5,7,请颠倒数组元素的存放位置并输出
注意:颠倒后的数组{7,5,3,9,5,1}
控制台输出:7 5 3 9 5 1
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {1,5,9,3,5,7};
for (int i = 0; i < arr.length/2; i++) {
int a = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = a;
}
System.out.println(Arrays.toString(arr));
}
}

#3、求出数组{15,35,5,-20,60}中的最大值和最小值
控制台输出:
最大值:60
最小值:-20
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {15,35,5,-20,60};
int max = arr[0],min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
}
if (min > arr[i]){
min = arr[i];
}
}
System.out.println("最大值是:"+max);
System.out.println("最小值是:"+min);
}
}

#4、键盘输入10个数,存到数组中,打印奇数位元素的值
注意:如果数组是{10,11,12,13,14,15},则奇数位元素是10,12,14
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
if ((i+1)%2!=0){
System.out.print(arr[i]+" ");
}
}
}
}

#5、有数组{1,22,0,3,44,0,0,5,66,7,0,0,0,88,9}
要求生成一个不包含0的新数组,并控制台遍历该数组
新数组:{1,22,3,44,5,66,7,88,9}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
int[] arr = {1,22,0,3,44,0,0,5,66,7,0,0,0,88,9};
List list = new ArrayList();
for (int i = 0; i < arr.length; i++) {
if (arr[i]!=0){
list.add(arr[i]);
}
}
Object[] brr = list.toArray();
System.out.println(Arrays.toString(brr));
}
}

边栏推荐
- Day38 LeetCode
- 4 模块三:文献阅读与研究方法
- [E-commerce operation] How to formulate a social media marketing strategy?
- Redis中RDB和AOF的区别
- Some common mysql entry exercises
- paddlepaddle implements CS_CE Loss and incorporates PaddleClas
- 【嵌入式开源库】使用J-Link打印日志,让你节省一个打印串口
- Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
- leetcode 9. Palindromic Numbers
- 什么是三次握手和四次挥手(清晰易懂)
猜你喜欢
随机推荐
4 Module 3: Literature Reading and Research Methods
CentOS7静默安装Oracle11g_转载
2022年质量员-土建方向-通用基础(质量员)考试模拟100题及在线模拟考试
ARM结构体系4:嵌入式硬件平台接口开发
Linux中安装redis
In the closing pages/uninstall (unload) sends a request to the server before the document
Oracle常用语句归纳_持续更新
[QNX Hypervisor 2.2 User Manual] 10.16 vdev virtio-blk
C语句:数据存储
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
【ARM】rk3399挂载nfs报错
ESP8266 教程3 — 通过TCP组建局域网并通信
2022建筑焊工(建筑特殊工种)考题及模拟考试
02.折叠隐藏文字
StarUML使用心得
【嵌入式开源库】cJSON的使用,高效精简的json解析库
关于ie下href有中文出现RFC 7230 and RFC 3986问题的研究
Some common mysql entry exercises
Let's talk programming languages together
Core Data 多线程设计







![[ARM] rk3399 mounts nfs error](/img/0c/f9f0a2f3850cd55d8bebbee7f898de.png)

