当前位置:网站首页>一维数组的应用
一维数组的应用
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]);
}
}
}

边栏推荐
- mysql出现不存在错误
- Technology vane | interpretation of cloud native technology architecture maturity model
- MySQL中文失败问题
- Common weak password Encyclopedia
- 榕树贷款,
- flask_ Reqparse parser inheritance in restful
- Six determination methods of Worthington peroxidase activity
- Kettle reads file split by line
- Two help points distribution brings to merchants
- Tool class of localdatetime sorted out by yourself
猜你喜欢

mysql如何优化
![[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

Quick sequencing and optimization

Vector to SVG method

Number of 0 at the end of factorial

Digital analog 1232

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

Design method and test method of APP interface use case

Briefly sort out the dualpivotquicksort

基于OpenCV的轮廓检测(1)
随机推荐
Wechat applet generation Excel
Smart pointer shared_ ptr、unique_ ptr、weak_ ptr
DNS record type and explanation of related terms
Spark: ranking statistics of regional advertising hits (small case)
一种分布式深度学习编程新范式:Global Tensor
Characteristics and determination scheme of Worthington pectinase
Program to change the priority of the process in LabVIEW
Penetration test - post penetration - Trace cleaning
[tree chain dissection] template question
Kettle reads file split by line
Database usage security policy
Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial
Spark Learning Notes (V) -- spark core core programming RDD conversion operator
mysql出现不存在错误
Introduction to redis
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Characteristics and determination scheme of Worthington pectinase
Explain工具实际操作
[regular] judgment, mobile number, ID number