Arrays commonly used classes

Methods Arrays class are static modifier to modify the static methods, when used can directly use the class name (Arrays) to call, instead of the object name to call (a "no" instead of "can not").

Specific features

To the array assignment: fill method

 public static void main(String[] args) {
        int[] a;
        a=new int[10];
        Scanner scanner=new Scanner(System.in);
        int val=scanner.nextInt();
        Arrays.fill(a,val);
        for(int i=0;i<a.length;i++)
            System.out.println(a[i]);
    }

 

 

Sorting array, sort methods, in ascending order;

        int[] b;
        b=new int[5];
        for(int i=0;i<b.length;i++)
            b[i]=scanner.nextInt();
        Arrays.sort(b);
        for(int i=0;i<b.length;i++)
            System.out.println(b[i]);

 

 

Comparison of arrays, equals the array elements are the same comparison method

        int[] c;
        c=new int[5];
        int[] d;
        d=new int[5];
        for(int i=0;i<5;i++)
            c[i]=d[i]=scanner.nextInt();
        boolean result=Arrays.equals(c,d);
        System.out.println(result);

 

 

Lookup array elements, the elements can be sorted on a binary search method by binarySearch.

        int [] = E new new  int [] {1,3,5,7,9,10 }; 
        System.out.println ( "array is:" + of Arrays.toString (E)); 
        System.out.println ( " 7 subscripts are: "+ Arrays.binarySearch (E, 7 )); 
        System.out.println ( " subscript is the 10: "+ Arrays.binarySearch (E, 10 )); 
        System.out.println ( " subscript 1 is: "+ Arrays.binarySearch (E, 1 )); 
        System.out.println ( " subscript 3 is: "+ Arrays.binarySearch (E, 3 )); 
        System.out.println ( " subscript 5 is: "+ Arrays.binarySearch (E, 5 )); 
        System.out.println ( " subscript 9 is: "+ Arrays.binarySearch (e, 9 ));

 

 There are too many classes and methods, and more look to API

 

Guess you like

Origin www.cnblogs.com/huangui/p/12663893.html