Java array

Java Array with Example

In this tutorial, We will learn how to work with arrays in Java with the help of examples, we will learn how to declare, initialize, and access array elements.

In Java, an array is a collection of elements with the same data type. An array’s elements are also stored in a single memory location. It’s a data structure where we keep similar items together. We can only store a certain number of elements in a Java arrays

Normally, an array is a collection of similar-type elements stored in a single memory location.

  • The 0th index is used to store the first element of an array, the 1st index is used to store the second element, and so on.
  • An array is a Java class object that is dynamically generated. The Serializable and Cloneable interfaces are implemented by Java arrays,which inherits the Object class. An array can be used to store values or primitive objects in Java. We can create single-dimensional and multidimensional arrays in Java, just like we can in C/C++.

Types of Array in java

Arrays can be divided into two types.

  1. Single Dimensional Array
  2. Multidimensional Array

In Java, the syntax for declaring an array is as follows:

dataType[] arrayName; (or)  
dataType  arrayName [];  

In Java, you can create an array.

datatype[] arrayName = new datatype[size];  

Example: Java Arrays

class Sample{  
public static void main(String args[]){  
int x[]=new int[5];
x[0]=10;   //initialization  
x[1]=20;  
x[2]=30;  
x[3]=40;  
x[4]=100;  
//traversing array  
for(int i=0;i<x.length;i++)
System.out.println(x[i]);  
}}  

Output:

10
20
30
40
100

Example: Java Arrays

We can declare, instantiate, and initialize the java arraysas a group by doing the following:

int x[]={3,4,5,6}; //declaration, instantiation and initialization  

Let’s take a look at another example of how to print this array.

class Sample{  
public static void main(String args[]){  
int x[]={3,4,5,6}; //declaration, instantiation and initialization  
for(int i=0;i<x.length;i++) 
System.out.println(x[i]);  
}}  

Output:

3
4
5
6

Example: For each Loop for Java Arrays

The for-each loop can also be used to print the Java arrays.

The for-each loop in Java prints each element of the array one by one. It stores an array element in a variable before running the loop’s body.

The for-each loop’s syntax is as follows:

for(datatype var :array){  
//statements
}  

Let’s look at an example of using the for-each loop to print the elements of a Java arrays

class Sample{  
public static void main(String args[]){  
int arr[]={3,4,5,6};  
for(int i:arr)  
System.out.println(i);  
}}  

Output:

3
4
5
6

Example: Anonymous Array in Java

An anonymous array is a feature of Java that eliminates the need to declare an array when passing it to a method.

public class Sample{  
static void printArray(int arr[]){  
for(int i=0;i<arr.length;i++)  
System.out.println(arr[i]);  

}  
  
public static void main(String args[]){  
printArray(new int[]{10,20,30,40});
}} 

Output:

10
20
30
40

Example: ArrayIndexOutOfBoundsException

When traversing an array, the Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if the length of the array is negative, equal to, or greater than the array size. 

public class Sample{  
public static void main(String args[]){  
int arr[]={10,20,30,40};  
for(int i=0;i<=arr.length;i++){  
System.out.println(arr[i]);  
}  
}}  

Output:

10
20
30
40

Multidimensional Arrays

The arrays we’ve discussed so far are known as one-dimensional arrays. In Java, however, we can declare multidimensional arrays.

In this case, data is stored in a row and column index (also known as matrix form).

A multidimensional array is a collection of arrays. In other words, each element of a multidimensional array is an array in and of itself. As an example:

double[][] matrix = {{1.2, 4.3, 4.0}, {4.1, -1.1} };

Example: Multidimensional Java Arrays

Let’s look at a simple example of declaring, instantiating, initialising, and printing a two-dimensional array.

class Sample{  
public static void main(String args[]){  
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};  
for(int i=0;i<3;i++){  
 for(int j=0;j<3;j++){  
   System.out.print(arr[i][j]+” “);  
 }  
 System.out.println();  
}  
}}  

Copying a Java Arrays: Using the arraycopy() method of the System class, we can copy one array to another.

Example: Copying an Array in Java

class Sample {  
    public static void main(String[] args) {  
        //declaring a source array  
        char[] CF = { 'a', 'e', 'i', 'o', 'u', 'a', 'e',  
                'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u' };  
        char[] CT= new char[7];  
         System.arraycopy(CF, 5, CT, 0, 8);  
         System.out.println(String.valueOf(CT));  
    }  
}  

Output:

aeiou

You might Like:

Java Break Statement with Example

We hope that this article will assist you in understanding all about java arrays. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, and we will be glad to assist you.

This Post Has One Comment

Leave a Reply