Arrays class


            Some methods below:  asList(...),  binarySearch(...), equals(...),  fill(...),  sort(...)


 The Arrays class lives in java.util.  It provides utilities for manipulating arrays, just as the Collections class provides utilities for manipulating collections.


 Another handy and important array manipulation method happens to live elsewhere, in the System class.  It is  arrayCopy(...) .



List  asList( Obj[ ] )  method


  Creating Lists from arrays.  This method converts an Array of objects to a List. (Note: not an array of primitives.)


  The opposite of .asList(...) is the .toArray(...) method found in every Collection class. (see toArray(...) )


  It returns a fixed-size List eligible for the use of List methods, backed by data in the array.


  It doesn’t actually copy the array's data into the List.  It changes to the List “write through” mode to the array, changing the List when the array is changed.  i.e.


import java.lang.reflect.Array;

import java.util.*;

public class TestArray  {


                public static void main(String args[ ]) {


String[ ] array = {"You", "Me", "Her", "Him"};  


List  ourList;

ourList = Arrays.asList(array) ;

System.out.println("List length: " + ourList.size());

System.out.println("List contains: " + ourList);


System.out.println("Array length: " + array.length);

for (int i =0; i<Array.getLength(array); i++) System.out.println("Array is: " + Array.get(array, i));

System.out.println(" ");


ourList.set( 1, "XXXXX");   // change AN OBJECT in THE LIST

//ourList.add("YYYYY");      // ADD AN OBJECT TO THE LIST


System.out.println("List length now: " + ourList.size());

System.out.println("List now contains: " + ourList);

System.out.println("Array length now: " + array.length);

for (int i =0; i<Array.getLength(array); i++) System.out.println("Array is now: " + Array.get(array, i));




int  binarySearch( type[ ],  key )  method


  The target array must already be sorted or you get undefined results.   No error is thrown however.


  If multiple results are possible, you may get any or none of them.  No error however.


  It returns the int index of the found item, if one is found.


  It always returns a negative int if something is not found. It returns the negative of the desired key's correct prospective insertion point, minus 1..



boolean equals( array1[  ],  array2[  ] )  method


  It returns a boolean.  Works for all types of arrays, both of primitives and objects.


  It overrides equals(...) for arrays.


  Comparing two null array references are always considered equal and will return true.


  This method produces the same results as the following code:


                        if( java.util.Arrays.equals( array1,  array2 ) );



void fill( array[ ], value )  method


  fill(...) has versions for all the primitive and boolean array types, plus objects.


  It puts the specified value into each cell of the specified array.



void sort( array[  ],  from,  to )   method


  sort(...) provides versions for all the primitive and boolean array types, plus objects.


  The from index starts at zero and the to index goes up to position to minus one. 

i.e. This code prints the ascending sorted sequence   1 2 3 4 


import java.util.*;


List  aList;

String[ ] array = {"4", "3", "2", "1"};

Arrays.sort (array,  0, 4);

List aList = Arrays.asList(array) ;



  There is no sorting if from = to.  No error is returned.


  Any equal elements which are found are not reordered.


  Both the int from and int to are optional. 

i.e.  Arrays.sort (array);  works the same as the  Arrays.sort (array,  0, 4);  example shown above.


  sort(Obj[ ], Comparator)  and sort(Obj[ ], from, to, Comparator) both let you name a comparator.


  To sort and reverse the order of an array, use this:  Arrays.sort(yourarray, Collections.reverseOrder( ));