当前位置:网站首页>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));
}
}
边栏推荐
- Some common mysql entry exercises
- Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
- Thymeleaf
- ARM Architecture 4: Embedded Hardware Platform Interface Development
- Switches and routers technology - 24 - configure OSPF single area
- [FPGA tutorial case 50] Control case 2 - FPGA-based PD controller verilog implementation
- [Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module
- Difference between @Resource and @Autowired
- Unity WebGL RuntimeError: integer overflow(整数溢出问题)
- Four functional interfaces
猜你喜欢
【嵌入式开源库】cJSON的使用,高效精简的json解析库
Optimization is a kind of habit low starting point is the "standing near the critical"
Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!
分库分表之sharding-proxy
网络协议1
C language antithesis: who is the murderer!
StarUML使用心得
ARM Architecture 4: Embedded Hardware Platform Interface Development
MySQL必知必会(初级篇)
C statement: data storage
随机推荐
Let's talk programming languages together
[E-commerce operation] How to formulate a social media marketing strategy?
2022年质量员-土建方向-通用基础(质量员)考试模拟100题及在线模拟考试
MySQL必知必会(初级篇)
[No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
In the closing pages/uninstall (unload) sends a request to the server before the document
宝塔Linux环境下redis开启多端口
The use of async (asynchronous) and await
3 Module 2: Use of scientific research tools
2022建筑焊工(建筑特殊工种)考题及模拟考试
IDEA使用记录
什么是三次握手和四次挥手(清晰易懂)
C language antithesis: who is the murderer!
Switches and routers technology - 21 - RIP routing protocol
Switch and Router Technology-29-OSPF Virtual Link
阿里云无法远程连接数据库MySQL错误码10060解决办法_转载
redis连接idea
How to read a paper
HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster