Java 2 added a new class or java.util called Arrays. This class provides various methods that <Ire useful when working with <arrays, Although these methods technically aren't part of Collections framework, they help bridge the gap between collections and arrays, Each method defined by Arrays is examined in this section. The as List( ) method returns a List that is backed by <Specified array.In other words,
both the list and the Tay refer to the same location. It has the following signature: static List,as List(Object Jeremy) Here, army is the army that contains the data,

The binary Search ) method uses a billiard search to find a specified value. This method must be applied to sorted arrays. It has the following forms: static int binary Search(byte array, byte value) static int binary Search(char[ army, char value; ,static int binary Search double] J army, double val llc) " static int binarySearch(f1oat[ ] array, float value) static int binarySearch(int[ ] army, int value) static int binarySearch(lonw'] army, long valllc) static int binarySearch(short{ ] array, short value) " static int binarySearch(Obje~l[ ) array, Object value) static int binarySearch(Object[ ] army, Object value, Comparator c) Here, arnu) is the array to be searched and uac is the value to be located, The Iast two forms throw a ClassCastexception if II/TO}I contains elements that cannot be compared (for example, Double and String Buffer) or if tuluc is not compatible with the types in army. In the 1.1St form, the Comparator c is used to determine the order of the elements in army. In all cases, if value exists in array, the index of the element is returned. Otherwise, <I negative value is returned. , The equals ) l'\-'If?method returns true if two arrays are equivalent, Otherwise, it returns false. The quest ) mctl.od has the following forms: static boolean cqu.ilsfboolcnn armyl[ I,boolean arrny2[]) , static boolean cqualsfbyte army1[ J, byte llr 2[ ]) static boolean equals(char array char array 2[ ]) static boolean equals(double array[ ], double array 2[ ]) static boolean equals float array[ ], float nrrn 2[ ]) static boolean equals(int array[ ], int array2[ ]) static boolean equals long array[ ]~long nrrn 2[ ]) static boolean equals short array[ ], short array2[ ]) static boolean equals(Object arg1[ ], Object array2[ ]) Here, army1 and army2 ore the two arrays that me compared for equality. The fill() method assigns a value to all elements in an array. In other words, it fills an array with a specified value. The fi1l() method has two versions. The first version, which has the following forms, fills an entire array:

static void fill(boolean arrny[ ], boolean wme)
static void fill(byte arrny[ ], byte value)
static void fill char Array( ], char vall/e)
static void fill(doubLe arrny[ ], double vIlI/l£')
static void fill(fLoat army[ ], float vallie)
static void fill(int nr~ay[ ], int vallie)
static void fill (long merrilY[ J, long vallie)
static void fill(short trendy[ ], short valllc)
static void fH1(Object ?array[ ], Object vaillc)

Here, value is assigned to all clements in army . The second version of the fiI1() method assigns a value to a subset of an arr:1y. Its forms are shown here:

static void fill(boolean arrnlf[ ], int start, int end, boolean value)
static void fill(byte amiy[ ], int start, int end, byte vall/e)
.static void filJ(cJla .•·arrny[ ), int start, int end, char.value)
static void !Ul(dOuble army[ ], int start, int end, double valtte)
static void fiU(fI,oat arrny[ ], int start, int end, float vlIllle)
static void fill(int arrny[ ], in] start, int end, int value)
static void fill(long armyl], int start, int end, longvIlle)
static void fill(short army[ j, int start, int.I.'/II/, short vall/C!)
static void fill (Object army[ 1, int start, int end, Object vallie)

Hen>, Vlll is assigned (0 the clements in nry from position start to position emf ..-I. These methods may all throw an I leg Ally'~reception if $misfiles greater Elan end, or an Array Index Out Of Bounds Exception if seal or ·lId is out of bounds. The sort( ) method sorts ill" array so thr.l it is arranged ill ascending order. The set ) method has two versions. 111e first version, shown here, sorts the entire ar!;'1Y:

static void sort(byte arrny[ J)
static void sort fc nur array[ D
static void sort double array[ ]) .
static void sort(float array[ ])
static void sort(int array[ ]) .
static 'Void Sort(long array]) .
static void sort(short.array[ ])
static void sort(Object array[ ]).
static void sort(Object array[ ], Comparator c)

Here, array is the array to be sorted. In the last form, c is a Comparator that issue too , order the elements of array. The forms that sort armies of Object can also throw a "ClassCastException.if elements of the array being sorted are not comparable. ' . - The second version of sort() enables you to specify a range within an array that  you want to sort; Its forms are shown here: , .

static void sort(byte array[ }, int start, int Clld)
static void sort(char array[ ], int start, int end)
static void sort double array[ ], int start, int clld)
static void sort(float array[ ], int start, int end;
static void sort(int array[ l, int start, int end)
static void sort(long Array[ ], int start, int cml)
static void sort(short array[ ], int start, int )
static void sort (Object Array[ ], int start, int end) .
static void sort Object army{ 1, Int start, int end, Comparative )

Here, the range beginning at start and running through end-A within array will be sorted. In the last form, c i&a Comparative that is used to order the elements or sorry. All of these methods can throw an illegal Argument Exception if start is greater than end, or an Array Index Out £Exception if start or end is out of bounds. The last two forms can also throw a Class Cast Exception if elements of the array being sorted are not comparable.

The following program illustrate how to use some of the methods of the Arrays class: ,- The following is the output from this program:

Share This