Home >>Java Tutorial >Java Arrays
Java array are basically an object that consists of the elements that are of a similar data type and these elements of an array are commonly stored in a contiguous memory location. This is generally a data structure where the programmers store the similar elements. The programmers or the users generally can store only a fixed set of elements in a Java array. Array in Java are known to be index-based, as they are stored according to one’s index.For instance, the first element of the array is generally stored at the 0th index, 2nd element is stored on 1st index and cycle continues. The programmers or users can get the length of the array just by using the length member that cannot be possible in C/C++ as in these language there is a need to use the sizeof operator.
Arrays are basically an object of a dynamically generated class in the Java language. And these arrays are known to inherit the Object class, and implements the Serializable as well as Cloneable interfaces. The programmers can store the primitive values or the objects in an array in the Java language. The programmers can also create single dimensional or multidimensional arrays in Java just like it happens in the C/C++ language.
In addition to that the Java delivers the feature of anonymous arrays that is not available in the C/C++ language. Java basically delivers a data structure, the array that stores a fixed-size sequential collection of elements that are of the same type. An array is used In order to store a collection of data, the arrays are used in the Java language but it is often more useful when you think of it as a collection of variables that are of the same type. The programmers can declare one array variable like numbers and use numbers[0], numbers[1], and ..., numbers[99] in order to represent the individual variables instead of declaring the individual variables.
In short you can understand the arrays as a function in Java that are basically used to store multiple values in a single variable eliminating the need of declaring separate variables for each value.
In order to use an array in the program, the programmer must declare a variable in order to reference the array, and he/she must specify the type of the array that a variable can reference. Here is the syntax for declaring an array variable depicted below:
dataType[] arrayRefVar; // preferred way. or dataType arrayRefVar[]; // works but not preferred way.
There are generally two types of array that are present in the Java language.
This is basically a collection of variables that are of the same type that is used by a common name.
Syntax to Declare an Array in Java
dataType[] arr; (or) dataType []arr; (or) dataType arr[];
class ArrayEx1 { public static void main(String args[]) { int arr[]=new int[5]; arr[0]=10;//initialization arr[1]=20; arr[2]=30; arr[3]=40; arr[4]=50; for(int i=0;i<arr.length;i++) { System.out.println(arr[i]+" "); } } }
While processing the array elements, the programmers must use either the for loop or the foreach loop and the reason being is all of the elements that are in an array are of the same type and the size of the array is known to the user.
Here is an example to Sum of array
public class ArrayEx2 { public static void main(String[] args) { int[] arr = {10,11,12,13}; int total = 0; // Print all the array elements for (int i = 0; i < arr.length; i++) { total += arr[i]; } System.out.println("Sum of given arrray = " + total); // Finding the largest element /* double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); */ } }
Here is an example to Find the maximum value of array
public class ArrayEx3 { public static void main(String[] args) { int[] arr = {10,11,12,13}; int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } System.out.println("Max value of array = " + max); } }
JDK 1.5 introduced a new for loop that is generally known as the foreach loop or enhanced for loop was introduced by the JDK 1.5 version of java that basically enables the programmers to traverse the complete array sequentially without even using an index variable.
Here is an example of the same that will depict all the elements in the array my list:
public class ArrayEx4{ public static void main(String[] args) { int[] arr = {10,11,12,13,14,15}; // Print all the array elements for (int element: arr) { System.out.println(element); } } }
As the programmers can pass primitive type values to the methods, just like that they can also pass the arrays to methods.
Here is an example depicted below that depicts the elements in an int array:
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }
An array can also be returned by a method. Here is an example that is depicting the method that returns an array that is the reversal of another array:
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }
In Multidimensional Array in Java, the data is generally stored in a row and column based index that is also known as matrix form.
Syntax to Declare Multidimensional Array in Java
dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[];
Here is an example of the Multidimensional array in Java as depicted below that will explain the procedure to declare, instantiate, initialize and print the 2Dimensional array for your understanding:
class ArrayEx5{ public static void main(String args[]) { //declare and initialize array int array[][]={{10,11,12},{13,14,15},{16,17,18}}; //print 2-D array for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(array[i][j]+" "); } System.out.println(); } }}
Here is an example of the Multidimensional array to Sum of 2D array
class ArrayEx6{ public static void main(String args[]) { //declare and initialize array int array[][]={{10,11,12},{13,14,15},{16,17,18}}; int sum=0; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { sum+=array[i][j]; } } System.out.print("Sum of 2D array="+sum); }}
The java.util.Arrays class generally consists of various static methods that are for sorting and comparing arrays, searching arrays, and filling array elements. These are the methods that are overloaded for all the primitive types.
Here is the list of the classes with their method and a short descriptions:
Method | Description |
---|---|
public static int binarySearch(Object[] a, Object key) | This method generally searches for the specified array of an Object ( Byte, Int , double, etc.) for the specified value just by using the binary search algorithm. Please note that the array must be sorted before making this call. In case, it is contained in the list then it will return index of the search key; otherwise, it will return ( – (insertion point + 1)). |
public static boolean equals(long[] a, long[] a2) | This method is generally used to return true if the two specified arrays of longs are found to be equal to one another. In Java language the two arrays are considered equal if both the arrays consists of the same number of elements and all the corresponding pairs of the elements that are in the two arrays are found to be equal. If this condition happens then it returns true when the two arrays are equal. |
public static void fill(int[] a, int val) | This method is generally used to assign the specified int value to each of the element of the specified array of ints. This same method could be generally used by all the other primitive data types (Byte, short, Int, etc.) |
public static void sort(Object[] a) | This method is generally used to sort the specified array of the objects into an ascending order that are according to the natural ordering of its elements. |