- Arrays Class
- Sorting of Arrays
- Binary Search for already sorted array
- Equals check of an array (.equals overridden method is called)
- parallelPrefix() – All all the numbers consecutively
- fill() – Assigns a default value to the complete array
- copyOf() – Copies one array to another
- asList – Converts arrays to list
- hashcode – returns the hashcode
- ArrayUtils (Pending Examples)
- add() – Used to add two arrays
- insert() – at a specific index
- clone
- contains
- get – safe from index out of bound exception
- indexOf – find index of an elemenet
- isEmpty
- isNotEmpty
- isSameLength
- isSorted
- lastIndexOf
- nullToEmpty – null reference of array is converted to empty array
- remove – Remove element at index passed – All subsequent elements are shifted to the left
- removeElement – removes element passed- All subsequent elements are shifted to the left
- removeAll
- removeAllOccurance
Array Class Example :
package com.demo.collections; import java.awt.Point; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { int[] numberArray = new int[] {54,11,23,88,39,10,51,14,77,30,91,10,53}; int[] unicode= new int[] {'V','b','D','p','O','K','A'}; String[] stringArray = new String[] {"xzy","xyz","pqr","abc","acb","xab","abc"}; int[] y; String[] z; //Sorting System.out.println("Original Number Array : "+Arrays.toString(numberArray)); y = Arrays.copyOf(numberArray,numberArray.length); //copy an array Arrays.sort(y);//sort an Array System.out.println("Completely Sorted : "+Arrays.toString(y)); y = Arrays.copyOf(numberArray,numberArray.length); Arrays.sort(y,0,5);//sort 0 to 5 System.out.println("First 5 sorted : "+Arrays.toString(y)); System.out.println("Original String Array : "+Arrays.toString(stringArray)); z = Arrays.copyOf(stringArray,stringArray.length); Arrays.sort(z); System.out.println("Completely Sorted : "+Arrays.toString(z)); Point[] points = {new Point(1,2), new Point(2,3), new Point(1,1)}; // Arrays.sort(points); //java.awt.Point cannot be cast to java.lang.Comparable, since comparable interface is required //Binary Search the array should be in sorted order y = Arrays.copyOf(numberArray,numberArray.length); Arrays.sort(y); System.out.println(Arrays.binarySearch(y,50));//-8 since at 8th location itshould have been present but it is not System.out.println(Arrays.binarySearch(stringArray,"abc")); //equals y = Arrays.copyOf(numberArray,numberArray.length); System.out.println(Arrays.equals(y,numberArray));//true Arrays.sort(y); System.out.println(Arrays.equals(y,numberArray));//false z = Arrays.copyOf(stringArray,stringArray.length); System.out.println(Arrays.equals(z,stringArray));//true Arrays.sort(z); System.out.println(Arrays.equals(z,stringArray));//false } }