当前位置:网站首页>一维数组的应用
一维数组的应用
2022-07-27 02:19:00 【黑白不纯】
1.求一组数的最大值
思路:从这组数中选取一个值,然后让它依次和其他的数进行大小比较.
public class Demo1 {
public static void main(String[] args) {
int[] sums = new int[]{1,5,777,43,5,577,666,778};
int max = sums[0];
for (int i = 0; i < sums.length; i++) {
if(max<sums[i]){
max=sums[i];
}
}
System.out.println(max);
}
}
2.在一组数中插入一个数
思路:先定义一个新数组,将原数组的值对应赋给新数组.然后确定插入的位置,从后往前,依次将前一个数的值赋给后一个数.
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
int[] a ={90,87,65,43,21};
int[] b =new int[a.length+1];
for (int i = 0; i <b.length-1 ; i++) {
b[i]=a[i];
}
System.out.println("插入一个数");
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
int index=b.length-1;
for (int i = 0; i <b.length; i++) {
if(c>b[i]){
index=i;
break;
}
}
for (int i = b.length-1; i>index ; i--) {
b[i]=b[i-1];
}
b[index]=c;
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
}
} 
3.冒泡排序-----数字升序排序
思路:两两相比小靠前,找到比较轮数与比较次数的关系.
//使用冒泡排序对输入的5名学员成绩进行升序排列
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
int[] student=new int[5];
Scanner sc = new Scanner(System.in);
System.out.println("请输入5名学生成绩");
for (int i = 0; i < student.length; i++) {
student[i]=sc.nextInt();
}
for (int i = 0; i < student.length-1; i++) {
for (int j =0 ; j <student.length-1-i ; j++) {
if(student[j]>student[j+1]){
int index=student[j];
student[j]=student[j+1];
student[j+1]=index;
}
}
}
for (int k = 0; k < student.length; k++) {
System.out.println(student[k]);
}
}
}

边栏推荐
- If the detailed explanation is generated according to the frame code
- 榕树贷款C语言结构体里的成员数组和指针
- redis入门练习
- Installation and use of anti-virus software ClamAV
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- C语言力扣第43题之字符串相乘。优化竖式
- Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
- It's confirmed that the registration of soft exam in the second half of 2022 will start in August
- Characteristics and determination scheme of Worthington pectinase
- Redis spike case, learn from Shang Silicon Valley teacher in station B
猜你喜欢

数据库概论 - 数据库的介绍
![[untitled] JDBC connection database read timeout](/img/24/726ed8b3419866244a1b69e6485d7c.png)
[untitled] JDBC connection database read timeout

477-82(236、61、47、74、240、93)

Contour detection based on OpenCV (1)

On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!

Solution to Chinese garbled code in console header after idea connects to database to query data

FastBoot brush machine
![[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree](/img/b3/58c053d082807a0e5639e5b968aa20.png)
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree

Code review pyramid

NLP hotspots from ACL 2022 onsite experience
随机推荐
app端接口用例设计方法和测试方法
Food chain (day 79)
Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)
面试题:String类中三种实例化对象的区别
Csu18m91 is used as the master controller of the intelligent scale scheme
百融榕树数据模型
Insert pictures and videos in typera
How to interact with the server when the client sends an SQL message
Daffodils (day 78)
一种分布式深度学习编程新范式:Global Tensor
Data analysis and disassembly method of banyan tree in Bairong
百融榕树数据分析拆解方法
在typora中插入图片和视频
ZJCTF_ login
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
Binary tree (day 82)
Worthington papain dissociation system solution
Spark Learning Notes (V) -- spark core core programming RDD conversion operator
Characteristics and determination scheme of Worthington pectinase
The function and application of lpci-252 universal PCI interface can card