Java Array

In this article, We will learn what is java array and how to work with it. We will learn array declaration in java, java initialize array, access array elements with the help of examples, and flowcharts.

Here is the table content of the article will we will cover this topic.
1. What is an Array in Java?
2. Important things about Array?

3. One dimensional array in Java?
4. Accessing of one dimensional Array?
5. Two-dimensional array in java?
6. Accessing of two-dimensional array?
7. Array of objects in java?
8. Arrayindexoutofboundsexception in java?

9. Jagged array in java?
10. Array default value in java?

What is an Array in Java?

An array is a container that can hold multiple values of one same data type. One array object can’t hold values of different data types. In other words, we can say the array is a collection of similar type of elements which have a contiguous memory location.

For example: If you want to hold 50 integer values, then you can create an array that can hold 50 values of int type. Instead of declaring individual variables, such as a1, a2, …, and a49, you declare one array variable that holds all the 50 values and you can access the values of an array by using a[0], a[1], and …, a[49].

Real-time Example: Let say, you want to store ID proof of 10 students. Then you can declare an array that can hold int type values. You don’t need to declare 10 individual variables of int type.

public class JavaArray
{ 
     public static void main(String args[])
     { 
    	 //Declaring 10 variables to store the 10 values
    	 int idProof1 = 11;
    	 int idProof2 = 2;
    	 int idProof3 = 123;
    	 int idProof4 = 042;
    	 int idProof5 = 015;
    	 int idProof6 = 026;
    	 int idProof7 = 037;
    	 int idProof8 = 034;
    	 int idProof9 = 01;
    	 int idProof10 = 9;
    	 
    	 System.out.print("Values from variables:");
    	 
    	 System.out.print(idProof1 + " ");
    	 System.out.print(idProof2 + " ");
    	 System.out.print(idProof3 + " ");
    	 System.out.print(idProof4 + " ");
    	 System.out.print(idProof5 + " ");
    	 System.out.print(idProof6 + " ");
    	 System.out.print(idProof7 + " ");
    	 System.out.print(idProof8 + " ");
    	 System.out.print(idProof9 + " ");
    	 System.out.print(idProof10 + " ");
    	 
    	 System.out.println();
    	 System.out.print("Values from variables:");
    	 
    	// Declaring an array to store the 10 values
    	 int idProofs[] = { 11, 2, 123, 042, 015, 026, 037, 034, 01, 9};
    	 
    	 for(int i = 0; i < idProofs.length; i++)
    		 System.out.print(idProofs[i] + " ");
     }
}

Output: Values from variables:11 2 123 34 13 22 31 28 1 9
Values from variables:11 2 123 34 13 22 31 28 1 9

Important things about Java array

  • An array can hold the value of the same data type. It never allows holding values of a different datatype. If you are declaring an array of int type it means it can hold only its values. It can’t take a value of String type etc.  
  • An array is stored in contiguous memory allocation. All the values are stored in order and each value placed on an index which starts from 0.
  • You can declare an Array as local if you are declaring it in the method.
  • The size of the array is specified only by int value. You can’t use any other data type to specify the size of the array.
  • Like other classes, Array also extends its superclass Object.
  • Every array type implements the interfaces Cloneable and java.io.Serializable.
  • An array can store primitive data types and Non-primitive data types.

Types of Array:

1. One-dimensional array: One Dimensional is always used with only one subscript([]). A one-dimensional array behaves likes a list of variables. You can access the variables of an array by using an index in square brackets preceded by the name of that array. Index value should be an integer. For more detail.

2. Multi-dimensional array: Two-dimensional array(2-D) is the simplest form of a multidimensional array. A two-dimensional array is the array of arrays because they store in tabular form. As like single dimensional array we must declare the two-dimensional array variable. We must specify the array name followed by two square brackets called subscript([][]). For more detail.

A two-dimensional array is stored in the tabular form of the row-column matrix. The two-dimensional array uses two subscripts([][]) where the first index behaves like row and second index column. The size of the matrix depends upon the size of the subscripts.

Java Array

1. Quiz, Read the below code and do answer.

An Array can store the ___ data type of elements.

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

The index of Array starts with _____?

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

Select a wrong way to declare the Array.

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

class MainClass
{
    public static void main(String arg[])
    {
        int a[] = new int[5];
        System.out.println("Value from : "+ a[3]);
    }
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

class MainClass
{
    public static void main(String arg[])
    {
        int a[][] = new int[5][5];
        System.out.println("Value from : "+ a[3]);
    }
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

How many subscripts uses by One dimensional Array.

Click on anyone to know the answer.

Leave a Comment