What Are Java Arrays?
An array in Java is a set of variables referenced by using a single variable name combined with an index number. Each item of an array is an element. All the elements in an array must be of the same type. Thus, the array itself has a type that specifies what kind of elements it can contain. An int array can contain int values, for example, and a String array can contain strings.
Java array is a data structure where we can store the elements of the same data type. The elements of an array are stored in a contiguous memory location. So, we can store a fixed set of elements in an array.
There are following ways to print an array in Java:
- Java for loop
- Java for-each loop
- Java Arrays.toString() method
- Java Arrays.deepToString() method
- Java Arrays.asList() method
1) Java for loop
It is used to execute a set of statements repeatedly until a particular condition is satisfied.
Syntax:
- for(initialization; condition; increment/ decrement)
- {
- //statements
- }
Example of for loop
In the following example, we have created an array of length four and initialized elements into it. We have used for loop for fetching the values from the array. It is the most popular way to print array in Java.
- public class PrintArrayExample1
- {
- public static void main(String args[])
- {
- //declaration and instantiation of an array
- int arr[]=new int[4];
- //initializing elements
- arr[0]=10;
- arr[1]=20;
- arr[2]=70;
- arr[3]=40;
- //traversing over array using for loop
- for(int i=0;i<arr.length;i++) //length is the property of the array
- System.out.println(arr[i]);
- }
- }
2) Java for-each loop
Java for-each loop is also used to traverse over an array or collection. It works on the basis of elements. It returns elements one by one in the defined variable.
Syntax:
- for(Type var:array)
Example of for-each loop
In the following example, we have created an array of String type of length four and initialized elements into it. We have used for-each loop to traverse over the array.
- public class PrintArrayExample2
- {
- public static void main(String args[])
- {
- // declaration and instantiation of an array
- String[] city = new String[4];
- //initializing elements
- city[0] = “Delhi”;
- city[1] = “Jaipur”;
- city[2] = “Gujarat”;
- city[3] = “Mumbai”;
- //traversing over array using for-each loop
- for (String str : city)
- {
- System.out.println(str);
- }
- }
- }
3) Java Arrays.toString() method
Java Arrays.toString() is a static method of Arrays class which belongs to java.util package It contains various methods for manipulating array.
Syntax:
- public static String toString(int[] a)
It accepts an array of any primitive type as an argument. It returns a string representation of an array that contains a list of array’s elements. The elements of an array are converted to String by String.valueOf(int) .
Example of toString() method
- import java.util.Arrays;
- public class PrintArrayExample3
- {
- public static void main(String[] args)
- {
- //declaring and initializing array
- int array[] = {34,-10, 56, -9, -33};
- //returns string representation of the specified array
- System.out.println(Arrays.toString(array));
- }
- }
4) Java Arrays.deepToString() method
The deepToString() method of Java Arrays class is designed for converting multidimensional arrays to strings.
Syntax:
- public static String deepToString(Object[] a)
It accepts an array as a parameter. It returns the String representation of an array.
Example of deepToString() method
In the following example, we have created a two dimensional array of float type.
- import java.util.Arrays;
- public class PrintArrayExample4
- {
- public static void main(String[] args)
- {
- //declaration and initialization of two dimensional array of float type
- float[][] array = {{1.2f, 2.5f}, {3.9f, 4.0f}, {5.3f, 6.2f}};
- System.out.println(Arrays.deepToString(array));
- }
- }
5) Java Arrays.asList() method
Java Arrays.asList() is a static method of Java Arrays class which belongs to java.util package. It act as a bridge between array based and collection based API.
Syntax:
- public static <T> List<T>asList(T…a)
The method also provides an easy way to create a fixed-size list initialize to contain many elements.
- List<T> obj=Arrays.toString(array[] a
It accepts an array as an argument. It returns the list view of an array.
Example of asList() method
- import java.util.Arrays;
- public class PrintArrayExample5
- {
- public static void main(String [] args)
- {
- //declaration and initialization of two dimensional array
- String[] stringArray={“Hello”,”Java”,”Programmers”};
- System.out.println(Arrays.asList(stringArray));
- }
Comments