Array数组的使用

 1 public class ArrayDemo {
 2     public static void main(String[] args) {
 3         int[] arr = {13,44,55,667,67,78};
 4         ArrayTool.printArray(arr);
 5         /*
 6          *  13, 
 7             44, 
 8             55, 
 9             667, 
10             67, 
11             78
12          */
13         int max = ArrayTool.getMax(arr);
14         System.out.println("max:"+max);
15         /*
16          * max:667
17          */
18         int index = ArrayTool.getIndex(arr, 55);
19         System.out.println("index:"+index);//index:2
20         
21         //与ArrayTool.printArray(arr);功能相同
22 //        for(int x = 0;x < arr.length;x++){
23 //            if(x == arr.length - 1){
24 //                System.out.println(arr[x]);
25 //            }else{
26 //                System.out.println(arr[x]+",");
27 //            }
28 //        }
29         
30         /*
31          *  13, 
32             44, 
33             55, 
34             667, 
35             67, 
36             78
37          */
38         
39     }
40 
41 
42 //    int max = ArrayTool.getMax(arr);
43 //    System.out.println("max:"+max);
44     //等价于
45 //    public static int getMax(int[] arr){
46 //        int max = arr[0];
47 //        
48 //        for(int x = 1;x < arr.length;x++){
49 //            if (arr[x] > max){
50 //                max = arr[x];
51 //            }
52 //        }
53 //        return max;
54 //    }
55 //    
56 //    
57 //    int index = ArrayTool.getIndex(arr, 55);
58 //    System.out.println("index:"+index);
59     //等价于
60 //    public static int getIndex(int[] arr,int value){
61 //        int index = -1;
62 //        
63 //        for(int x=0; x < arr.length;x++){
64 //            if(arr[x] == value){
65 //                index = x;
66 //                break;
67 //            }
68 //        }
69 //        return index;
70 //        
71 //    }
72 }

 

posted @ 2018-07-27 18:12  大数据和AI玩出的梗  阅读(226)  评论(0编辑  收藏  举报