数组

 

 1 package com.unit3.test;
 2 
 3 public class Test1 {
 4 
 5     public static void main(String[] args) {
 6         // 动态初始化
 7         int[] a=new int[5];
 8         a[0]=2;
 9         a[1]=2;
10         a[2]=2;
11         a[3]=2;
12         a[4]=2;
13         
14         //静态初始化
15         int[] b=new int[]{1,2,3,4,5};
16         int[] c= {6,7,8,9};
17         
18         String[] d={"a","b","c","d","e","f","d"};
19         System.out.println(d[5]);
20         System.out.println(d.length);
21     }
22 
23 }

 

 1 package com.unit3.test;
 2 
 3 public class Test2 {
 4 
 5     public static void main(String[] args) {
 6         // 数组遍历 用循环的方式将数组每一个元素取出
 7         int[] a= {19,22,45,10,88};
 8         
 9 //        int i=0;
10 //        while(i<=4) {
11 //            System.out.println(a[i]);
12 //            i++;
13 //        }
14         
15 //        double sum=0;
16 //        for(int j=0;j<=a.length-1;j++) {
17 //            System.out.println(a[j]);
18 //            sum+=a[j];
19 //        }
20 //        System.out.println(sum);
21 //        System.out.println(sum/a.length);
22         
23         double[] b= {1.11,2.33,34.1,232,4.56};
24         for(double x:b) {
25             System.out.println("当前循环的数字是:"+x);
26         }
27     }
28 
29 }

 

 1 package com.unit3.test;
 2 
 3 public class Test3 {
 4 
 5     public static void main(String[] args) {
 6         // 数组一旦初始化,长度无法改变
 7         
 8         //数组增加元素
 9 //        int[] oldArray= {1,2,3,4,5,6};
10 //        int[] newArray=new int[oldArray.length+1];  //最后插入0
11 //        for(int i=0;i<=oldArray.length-1;i++) {
12 //            newArray[i]=oldArray[i];
13 //        }
14 //        newArray[oldArray.length]=0;
15 //        for(int x:newArray) {
16 //            System.out.println(x);
17 //        }
18         
19         
20 //        int[] oldArray= {1,2,3,4,5,6};
21 //        int[] newArray=new int[oldArray.length+1];   //中间位置插入0,[1,2,3,0,4,5,6]
22 //        for(int i=0;i<=newArray.length-1;i++) {
23 //            if(i<3) {
24 //                newArray[i]=oldArray[i];
25 //            }else if(i==3) {
26 //                newArray[i]=0;
27 //            }else if(i>3) {
28 //                newArray[i]=oldArray[i-1];
29 //            }
30 //        }
31 //        
32 //        for(int x:newArray) {
33 //            System.out.println(x);
34 //        }
35         
36         //删除元素
37         int[] oldArray= {1,2,34,66,3,35,45};
38         int[] newArray=new int[oldArray.length-1];
39         for(int i=0;i<=newArray.length-1;i++) {
40             if(i<3) {
41                 newArray[i]=oldArray[i];
42             }else if(i>=3) {
43                 newArray[i]=oldArray[i+1];
44             }
45         }
46         
47         for(int x:newArray) {
48             System.out.println(x);
49         }
50 
51     }
52 
53 }

 

 1 package com.unit3.test;
 2 
 3 public class Test4 {
 4 
 5     public static void main(String[] args) {
 6         
 7         int[] a= {23,44,54,3,213,53};
 8         
 9         // 最大值
10         int max=a[0];
11         for(int x:a) {
12             if(x>max) {
13                 max=x;
14             }
15         }
16         System.out.println(max);
17         
18         // 最小值
19         int min=a[0];
20         for(int x:a) {
21             if(x<min) {
22                 min=x;
23             }
24         }
25         System.out.println(min);
26                 
27         //平均值
28         double sum=0.0;
29         for(int x:a) {
30             sum+=x;
31         }
32         System.out.println(sum/a.length);
33 
34     }
35 
36 }

 

 1 package com.unit3.test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test5 {
 6 
 7     public static void main(String[] args) {
 8         // 例子:商品管理
 9         
10         Scanner in=new Scanner(System.in);
11         
12         String[] numArray= {"1001","1002","1003","1004","1005"}; //保存商品编号
13         String[] nameArray= {"苹果","梨子","香蕉","芒果","葡萄"};   //保存商品名称
14         double[] priceArray= {2.5,3.6,1,5.6,10.7};  //保存商品价格
15         
16         for(int i=0;i<=numArray.length-1;i++) {
17             System.out.println(numArray[i]+"-----"+nameArray[i]+"-----"+priceArray[i]);
18         }
19         
20         while(true) {
21             System.out.println("请输入产品编号:");
22             String num=in.next();
23             
24             int msg=0;
25             for(String x:numArray) {
26                 if(num.equals(x)) {
27                     System.out.println("编号已经存在,请重新输入:");
28                     msg=1;
29                     break;
30                 }
31             }
32             if(msg==1) {
33                 continue;
34             }
35             
36             System.out.println("请输入产品名称:");
37             String name=in.next();
38             
39             for(String x:nameArray) {
40                 if(name.equals(x)) {
41                     System.out.println("名称已经存在,请重新输入:");
42                     msg=1;
43                     break;
44                 }
45             }
46             if(msg==1) {
47                 continue;
48             }
49             
50             System.out.println("请输入产品价格:");
51             double price=in.nextDouble();
52             
53             String[] numArray1=new String[numArray.length+1];
54             String[] nameArray1=new String[nameArray.length+1];
55             double[] priceArray1=new double[priceArray.length+1];
56             
57             for(int i=0;i<=numArray.length-1;i++) {
58                 numArray1[i]=numArray[i];
59                 nameArray1[i]=nameArray[i];
60                 priceArray1[i]=priceArray[i];
61             }
62             numArray1[numArray1.length-1]=num;
63             nameArray1[nameArray1.length-1]=name;
64             priceArray1[priceArray1.length-1]=price;
65             
66             System.out.println("编号--------------------名称---------------------价格");
67             for(int i=0;i<=numArray1.length-1;i++) {
68                 System.out.println(numArray1[i]+"-----"+nameArray1[i]+"-----"+priceArray1[i]);
69             }
70             
71             
72             break;
73         }
74 
75     }
76 
77 }

 

 1 package com.unit3.test;
 2 
 3 public class Test6 {
 4 
 5     public static void main(String[] args) {
 6         // 二维数组遍历
 7         
 8 //        int[][] a=new int[3][5];
 9 //        a[0][0]=1;
10         
11         int[][] a= {{1,2,3},{4,5,6,7,8,9,10},{11,22,33,44}};
12         //方法一遍历:
13 //        for(int i=0;i<a.length;i++) {
14 //            for(int j=0;j<a[i].length;j++) {
15 //                System.out.println(a[i][j]);
16 //            }
17 //        }
18         
19         //方法二遍历:
20         int count=0;
21         for(int[] x:a) {
22             for(int i:x) {
23 //                System.out.println(i);
24                 count++;
25             }
26         }
27         
28         //将遍历的数据加入新数组
29         int[] b=new int[count];
30         int s=0;
31         for(int[] x:a) {
32             for(int i:x) {
33                 b[s]=i;
34                 s++;
35             }
36         }
37         for(int x:b) { //遍历新数组
38             System.out.println(x);
39         }
40 
41     }
42 
43 }

 

 1 package com.unit3.test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class Test7 {
 6 
 7     public static void main(String[] args) {
 8         // Arrays工具类
 9         int[] a= {10,3,54,6545,1,23,55,5,24};
10         
11 //        //排序,升序
12 //        Arrays.sort(a);
13 //        for(int x:a) {
14 //            System.out.println(x);
15 //        }
16 //        
17 //        //降序
18 //        for(int i=a.length-1;i>=0;i--) {
19 //            System.out.println(a[i]);
20 //        }
21         
22         //将数字数组转为字符串数组
23         String b=Arrays.toString(a);
24         System.out.println(b);
25 
26     }
27 
28 }

 

posted on 2020-06-07 19:34  cherry_ning  阅读(74)  评论(0)    收藏  举报

导航