Java數(shù)組

中國大學Mooc學習 5.1

1.數(shù)組遍歷

1.for遍歷

int[] a = {1, 2, 45,6545,32423,3,4,5,6};

for (int s:a)
 {
            System.out.println(s);
  }

等價于:

for (int i =0;i<a.length;i++){
            System.out.print(a[i]);
        }

2.java數(shù)組小實例

寫一個程序,輸入數(shù)量不確定的【0,9】范圍內(nèi)的證書,統(tǒng)計每一種數(shù)字出現(xiàn)的次數(shù),輸入-1表示結(jié)束

System.out.print("請輸入一個數(shù):");
        Scanner in = new Scanner(System.in);

        int max;

        int[] num = new int[100];
        int x;//盛放輸入數(shù)據(jù)

        x = in.nextInt();//先賦給x一個值
        max = x;

        //將輸入值賦給數(shù)組
       for (int i=0;x!=-1;i++){
           if (x>max)
               max = x;//求的輸入最大值,在創(chuàng)立一個數(shù)組程度為最大值的數(shù)組,以統(tǒng)計出現(xiàn)個數(shù)
           num[i] = x;
           x = in.nextInt();//賦給x下一個值
       }

       int[] count = new  int[max+1];

        for (int i=0;i<num.length;i++){
            count[num[i]]++;
        }

        for (int i=0;i<count.length;i++){
            System.out.print(i+":");
            System.out.println(count[i]);

        }

這種想法是從《算法珠璣》中看到的

再次修改
System.out.print("請輸入一個數(shù):");
        Scanner in = new Scanner(System.in);

        int max;//計數(shù)

        int[] num = new int[100];
        //初始值全部賦值為-1,未來在最后一部過濾
        for (int i=0;i<num.length;i++){
            num[i] = -1;
        }

        int x;//盛放輸入數(shù)據(jù)

        x = in.nextInt();//先賦給x一個值
        max = x;

        //將輸入值賦給數(shù)組
       for (int i=0;x!=-1;i++){
           if (x>max)
               max = x;//求的輸入最大值,在創(chuàng)立一個數(shù)組程度為最大值的數(shù)組,以統(tǒng)計出現(xiàn)個數(shù)
           num[i] = x;
           x = in.nextInt();//賦給x下一個值
       }

       int[] count = new  int[max+1];


//        for (int i=0;i<count.length;i++){
//            count[num[i]]=0;//將有值的全部賦值為0,以便可以正確計數(shù)
//        }

        for (int i=0;i<num.length;i++){
            if (num[i]==-1){
//                System.out.println("contiune OK");
                continue;
            }
            count[num[i]]++;//計數(shù)
        }
        System.out.println("計數(shù) OK");

        for (int i=0;i<count.length;i++){

            if (count[i]==0){
                continue;//去除計數(shù)值為0的數(shù)組
            }

            System.out.print(i+":");
            System.out.print(count[i]+"     ");

        }

輸出結(jié)果:

請輸入一個數(shù):1 2 3 4 5 6 1 1 1  0  0 -1
計數(shù) OK
0:2     1:4     2:1     3:1     4:1     5:1     6:1  

請輸入一個數(shù):1 2 3 7 89 76 -1
計數(shù) OK
1:1     2:1     3:1     7:1     76:1     89:1 

再次再次修改
System.out.print("請輸入一個數(shù):");
        Scanner in = new Scanner(System.in);

        int max;//計數(shù)

        int[] num = new int[100];

        int x;//盛放輸入數(shù)據(jù)

        x = in.nextInt();//先賦給x一個值
        max = x;

        //將輸入值賦給數(shù)組
       for (int i=0;x!=-1;i++){
           num[x]++;
           x = in.nextInt();//賦給x下一個值
       }
       for (int i=0;i<num.length;i++)
       {
           if (num[i]==0){
               continue;
           }
           System.out.print(i+":"+num[i]+"    ");
       }

輸出結(jié)果:

請輸入一個數(shù):1 2 3 4 5 6 1 1 1  0  0 -1
0:2    1:4    2:1    3:1    4:1    5:1    6:1    

算法的重要性!

2.foreach

int[] a = {1, 2, 45,6545,32423,3,4,5,6};

for (int s:a)
 {
            System.out.println(s);
  }

2.二維數(shù)組

1.二維數(shù)組賦值

 for (int i=0;i<num.length;i++){
            for (int j =0;j<num[i].length;j++){
                num[i][j] = rand.nextInt(10);
                System.out.print(num[i][j]+"  ");
            }

2.二維數(shù)組遍歷

方法一:

for (int i=0;i<num.length;i++){
            for (int j =0;j<num[i].length;j++){
                System.out.print(num[i][j]+"  ");
            }

方法二:

for (int[] s:num){
            for (int i:s){
                System.out.print(i+"  ");
            }
            System.out.println("\n");

        }
int[][] num = new int[5][6];

        //random number
        Random rand = new Random(10);

        for (int i=0;i<num.length;i++){
            for (int j =0;j<num[i].length;j++){
                num[i][j] = rand.nextInt(10);
                System.out.print(num[i][j]+"  ");
            }
            System.out.println("\n");
        }
        System.out.println("-----------------------------\n" +
                "");
        for (int[] s:num){
            for (int i:s){
                System.out.print(i+"  ");
            }
            System.out.println("\n");

        }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容