Javatpoint Logo
Javatpoint Logo

Java Array

Normally, an array is a collection of similar type of elements that have a contiguous memory location.

Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.

Array in java is index-based, the first element of the array is stored at the 0 index.

java array

Advantages

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
  • Random access: We can get any data located at an index position.

Disadvantages

  • Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

Single Dimensional Array in Java

Syntax to Declare an Array in Java

Instantiation of an Array in Java

Example of Java Array

Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.

Test it Now

Output:

10
20
70
40
50

Declaration, Instantiation and Initialization of Java Array

We can declare, instantiate and initialize the java array together by:

Let's see the simple example to print this array.

Test it Now

Output:

33
3
4
5

Passing Array to Method in Java

We can pass the java array to method so that we can reuse the same logic on any array.

Let's see the simple example to get the minimum number of an array using a method.

Test it Now

Output:

3

Anonymous Array in Java

Java supports the feature of an anonymous array, so you don't need to declare the array while passing an array to the method.

Test it Now

Output:

10
22
44
66

Returning Array from the Method

We can also return an array from the method in Java.

Test it Now

Output:

10
30
50
90
60

ArrayIndexOutOfBoundsException

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

Test it Now

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at TestArrayException.main(TestArrayException.java:5)
50
60
70
80

Multidimensional Array in Java

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

Syntax to Declare Multidimensional Array in Java

Example to instantiate Multidimensional Array in Java

Example to initialize Multidimensional Array in Java

Example of Multidimensional Java Array

Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.

Test it Now

Output:

1 2 3
2 4 5
4 4 5

Jagged Array in Java

If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.

Test it Now

Output:

0 1 2 
3 4 5 6 
7 8 

What is the class name of Java array?

In Java, an array is an object. For array object, a proxy class is created whose name can be obtained by getClass().getName() method on the object.

Test it Now

Output:

I

Copying a Java Array

We can copy an array to another by the arraycopy() method of System class.

Syntax of arraycopy method

Example of Copying an Array in Java

Test it Now

Output:

caffein

Addition of 2 Matrices in Java

Let's see a simple example that adds two matrices.

Test it Now

Output:

2 6 8
6 8 10

Multiplication of 2 Matrices in Java

In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the second matrix which can be understood by the image given below.

Matrix Multiplication in Java

Let's see a simple example to multiply two matrices of 3 rows and 3 columns.

Test it Now

Output:

6 6 6 
12 12 12 
18 18 18 


Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials