数组的经典例子
例1:求学生的平均分
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
4 int scores []=new int[5];
5 int sum=0;
6
7 System.out.println("请输入五位同学的成绩:");
8 for (int i = 0; i < scores.length; i++) {//遍历数组
9 scores[i]=input.nextInt();
10 sum+=scores[i];//成绩累加
11 }
12 System.out.print("平均分:"+sum/scores.length);
例2:输出5笔购物金额及总金额
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 double sum=0;
4 double scores[]=new double[5];
5 for (int i = 0; i < scores.length; i++) {
6 System.out.print("请输入第"+(i+1)+"笔金额的记录:");
7 scores[i]=input.nextDouble();
8 sum+=scores[i];
9 }
10 System.out.println("序号\t\t"+"金额(元)");
11 for (int i = 0; i < scores.length; i++) {
12 System.out.print((i+1)+"\t\t");
13 System.out.println(scores[i]);
14 }
15 System.out.println("总金额:\t\t"+sum);
例3:循环录入5位学员成绩,进行升序排列后输出结果
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 int scores []=new int[5];
4 System.out.println("请输入5位同学的成绩:");
5 //录入成绩
6 for (int i = 0; i < scores.length; i++) {
7 scores[i]=input.nextInt();
8 }
9 Arrays.sort(scores);//排序
10 System.out.println("学员成绩按升序排序:");
11 for (int i = 0; i < scores.length; i++) {
12 System.out.println(scores [i]+" ");
13 }
例4:从键盘输入本次Java考试五位学生的成绩,求考试成绩最高分
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 int scores []=new int[5];
4 int max=0;
5 System.out.println("请输入5位同学的成绩:");
6 //录入成绩
7 for (int i = 0; i < scores.length; i++) {
8 scores[i]=input.nextInt();
9 }
10
11 for (int i = 0; i < scores.length; i++) {
12 if (scores[i]>max) {
13 max=scores[i];
14 }
15 }
16 System.out.println("最高分:"+max);
17 }
例5:有一组学员的成绩{99,85,82,63, 60},将它们按升序排列。要增加一个学员的成绩,将它插入成绩序列,并保持升序。
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 int [] list=new int [6];
4 list[0]=99;
5 list[1]=95;
6 list[2]=92;
7 list[3]=89;
8 list[4]=69;
9 list[5]=49;
10 int index=list.length;//保存新增成绩的位置
11 System.out.println("请输入新增成绩:");
12 int num=input.nextInt();//输入要插入的数据
13 //找到新元素插入的位置
14 for (int i = 0; i < list.length; i++) {
15
16 if (num>list[i]) {
17 index=i;
18 break;
19 }
20 }
21 //元素后移
22 for (int i =list.length-1 ; i >index ; i--) {
23 list[i]=list[i-1];//index下标开始的元素后移一个位置
24 }
25 list[index]=num;
26 System.out.println("插入成绩的下标:"+index);
27
28 System.out.println("插入后成绩信息是:");
29 for (int i = 0; i < list.length; i++) {
30 System.out.println(list[i]+"\t");
31 }
32 }
例6:将 一组乱序的字符进行排序 进行升序和逆序输出
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 String[]num=new String[]{"a","c","u","b","e","p","f","z"};
4 System.out.print("原字符序列:");
5 for (int i = 0; i < num.length; i++) {
6 System.out.print(num[i]+" ");
7 }
8 Arrays.sort(num);
9 System.out.println();//换行
10 System.out.print("升序排序后:");
11 for (int i = 0; i < num.length; i++) {
12 System.out.print(num[i]+" ");
13 }
14 System.out.println();//换行
15 System.out.print("逆序输出为:");
16 //逆序,则从最后的哪一个元素排在第一位
17 for (int i = num.length-1; i >=0 ; i--) {
18 System.out.print(num[i]+" ");
19 }
例7:求出4家店的最低手机价格及原始位置(下标)
1 Scanner input=new Scanner(System.in);
2 System.out.println("请输入4家店的价格");
3 int[]num=new int[4];
4 for (int i = 0; i < num.length; i++) {
5 System.out.print("第"+(i+1)+"店的价格:");
6 num[i]=input.nextInt();
7 }
8 int min=num[0];
9 int index=0;
10 for (int j = 0; j < num.length; j++) {
11 if (num[j]<min) {
12 min=num[j];
13 index=j;
14 }
15
16 }
17 System.out.print("最低价格:"+min);
18 System.out.println("且它在数组中的原始位置(下标)是:" + index);
19 }
20 }
例8:从键盘上输出10个整数,合法值为1,2或3,其余为不合法,并且统计合法及不合法的个数
1 Scanner input=new Scanner(System.in);
2 int nums[] = new int[10];
3 int a = 0;
4 int b = 0;
5 int c = 0;
6 int d = 0;
7 System.out.println("请输入10个数:");
8 for (int i = 0; i < nums.length; i++) {
9 nums[i] = input.nextInt();
10
11 switch (nums[i]) {
12 case 1:
13 a++;
14 break;
15 case 2:
16 b++;
17 break;
18 case 3:
19 c++;
20 break;
21 default:
22 d++;
23 break;
24 }
25
26 }
27 System.out.println("数字1的个数:"+a);
28 System.out.println("数字2的个数:"+b);
29 System.out.println("数字3的个数:"+c);
30 System.out.println("非法数字的个数:"+d);
例9:假设有一个数组,长度为5,int [] aray=new int[]{1,3,-1,5,-2},先创建一个新数组,要求新数组的存放顺序与原数组的元素逆序,并且如果原数组中的元素值小于0,在新数组中安0存储,
1 Scanner input=new Scanner(System.in);
2
3 int []array=new int[]{1,3,-1,5,-2};
4 System.out.println("原数组的为:");
5 for (int i = 0; i < array.length; i++) {
6 System.out.print(array[i]+ " ");
7 }
8 System.out.println();
9 int newarray[]=new int[5];
10 for (int i = array.length - 1; i >= 0; i--) {
11 if (array[i] < 0) {
12 continue;
13 }
14 if (array[i] > 0) {
15 newarray[array.length - i - 1] = array[i];
16 }
17 }
18 System.out.println("");
19 System.out.println("逆序并处理后的数组为:");
20 for (int i = 0; i < newarray.length; i++) {
21 System.out.print(newarray[i]+" ");
22 }
23 }
例10:
1 public static void main(String[] args) {
2 Scanner input=new Scanner(System.in);
3 String[] musics = new String[]{"Island","Ocean","Pretty","Sun"};
4 int index = musics.length; //保存新歌插入位置
5 //输出插入前的结果
6 System.out.print("插入前的数组为:");
7 for(int i = 0; i < musics.length ; i++){
8 System.out.print(musics[i]+" ");
9 }
10 //新数组
11 String[] newMusics = new String[musics.length+1];//新歌曲数组
12 String music = ""; //保存用户输入的歌曲名称
13 //将数组musics中的元素复制到新歌曲数组newMusics中
14 for(int i = 0; i < musics.length; i++){
15 newMusics[i] = musics[i];
16 }
17 //输入歌曲名称
18 System.out.print("\n请输入歌曲名称:");
19 music = input.nextLine();
20 //找到新元素的插入位置
21 for(int i = 0; i < musics.length; i++){
22 if(musics[i].compareToIgnoreCase(music) > 0){
23 index = i;
24 break;
25 }
26 }
27 //元素后移
28 for(int i = newMusics.length-1; i > index; i--){
29 newMusics[i] = newMusics[i-1]; //index下标开始的元素后移一个位置
30 }
31 newMusics[index] = music; //新元素放在index的位置 地址
32 System.out.print("逆序处理后的数组为:");
33 for (int i = 0; i < newMusics.length; i++) {
34 System.out.print(newMusics[index]+" " );
35 }
36
37 }
38
39 }