12.2 Java Collection Framework for Arrays
Monday, August 24, 2009
The Java collection framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.
A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.
The classes and interfaces of the collections framework are in package java.util.
In this post we are going to discuss how arrays are associated with Java collection framework and also how to implement various methods of the class Arrays.
Class Arrays provides static methods for manipulating arrays, including sort for sorting an array, binarySearch for searching a sorted array, equals for comparing arrays and fill for placing items in an array.
So, have a look at the fallowing program to implement various methods of the class Arrays.
Sorting Example
As we can see in the above example, we sorted the array by the help of a static method of Arrays class. Where we just passed an array argument to the static method sort.When we compile this program we can get the fallowing output.
Enter Elements into the Array...
78
-5
94
22
71
The Elements in the Array are...
78 -5 94 22 71
After Sorting the Array...
-5 22 71 78 94
Implementing Binary Search
When we compile the above program we can see the fallowing output.
Enter Elements into the Array....
10
20
30
40
50
The Sorted Array is....
10 20 30 40 50
Enter an Element to Search :
40
Element Found at Location : 4
Note : Binary Search only works with Sorted Array of elements
fill, arrayCopy and equals
When we compile the above program we can see the fallowing output.
Enter Elements into A ....
10
20
30
40
50
The Elements in the Array are ...
10
20
30
40
50
After Filling the Array with 5s...
5
5
5
5
5
The Values in the Array B are ....
5
5
5
5
5
Both Array A and B are Equal
In Java API, various methods are available for Arrays to manipulate different types of data like int, float, double and etc. We can implement those methods whenever we want.
A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.
The classes and interfaces of the collections framework are in package java.util.
In this post we are going to discuss how arrays are associated with Java collection framework and also how to implement various methods of the class Arrays.
Class Arrays provides static methods for manipulating arrays, including sort for sorting an array, binarySearch for searching a sorted array, equals for comparing arrays and fill for placing items in an array.
So, have a look at the fallowing program to implement various methods of the class Arrays.
Sorting Example
// Sorting an Array import java.util.Scanner; import java.util.Arrays; /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a[]; a=new int[5]; Scanner s=new Scanner(System.in); System.out.print("Enter Elements into the Array..."); for(int i=0;i<a.length;i++) { a[i]=s.nextInt(); } System.out.println("The Elements in the Array are..."); for(int i=0;i<a.length;i++) { System.out.printf("%d\t", a[i]); } System.out.println(); // passing array to the static method sort Arrays.sort(a); System.out.println("After Sorting the Array..."); System.out.println(); for(int i=0;i<a.length;i++) { System.out.printf("%d\t", a[i]); } } }
As we can see in the above example, we sorted the array by the help of a static method of Arrays class. Where we just passed an array argument to the static method sort.When we compile this program we can get the fallowing output.
Enter Elements into the Array...
78
-5
94
22
71
The Elements in the Array are...
78 -5 94 22 71
After Sorting the Array...
-5 22 71 78 94
Implementing Binary Search
// Using BinarySearch in an Array import java.util.Scanner; import java.util.Arrays; /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a[]; a=new int[5]; Scanner s=new Scanner(System.in); System.out.println("Enter Elements into the Array...."); for(int i=0;i<a.length;i++) { a[i]=s.nextInt(); } //Sorting the Array //BinarySearch only works with Sorted Elements Arrays.sort(a); System.out.println("The Sorted Array is...."); for(int i=0;i<a.length;i++) { System.out.printf("%d\t", a[i]); } System.out.println(); System.out.println("Enter an Element to Search : "); int key=s.nextInt(); // binarySearch returns a positive integer if element found int flag=Arrays.binarySearch(a, key); // the return value is the position of the element in the array if(flag >=0) { System.out.printf("Element Found at Location : %d", flag+1); } else { System.out.println("Element Not Found !"); } } }
When we compile the above program we can see the fallowing output.
Enter Elements into the Array....
10
20
30
40
50
The Sorted Array is....
10 20 30 40 50
Enter an Element to Search :
40
Element Found at Location : 4
Note : Binary Search only works with Sorted Array of elements
fill, arrayCopy and equals
// Implementing fill, equals and arrayCopy import java.util.Scanner; import java.util.Arrays; /** * * @author Sudarsan */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a[]; int b[]; a=new int[5]; b=new int[5]; Scanner s=new Scanner(System.in); System.out.println("Enter Elements into A ...."); for(int i=0;i<a.length;i++) { a[i]=s.nextInt(); } System.out.println("The Elements in the Array are ..."); for(int i:a) System.out.println(i); //Filling the Array A with 5 Arrays.fill(a, 5); System.out.println("After Filling the Array with 5s..."); for(int i:a) System.out.println(i); // Copying the values of A to B System.arraycopy(a, 0, b, 0, 5); System.out.println("The Values in the Array B are ...."); for(int i:b) System.out.println(i); // Testing wether both Arrays are equal or not if(Arrays.equals(a, b)) { System.out.println("Both Array A and B are Equal"); } else { System.out.println("A and B are Not Equal"); } } }
When we compile the above program we can see the fallowing output.
Enter Elements into A ....
10
20
30
40
50
The Elements in the Array are ...
10
20
30
40
50
After Filling the Array with 5s...
5
5
5
5
5
The Values in the Array B are ....
5
5
5
5
5
Both Array A and B are Equal
In Java API, various methods are available for Arrays to manipulate different types of data like int, float, double and etc. We can implement those methods whenever we want.
Labels:
Arrays