Java Tutorial

What is Java


Control Statements


Java Object Class

Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java Array

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

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

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

Advantage of Java Array

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

Disadvantage of Java Array

  1. Size Limit: We can store only 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.

Types of Array in java

There are two types of array.

  1. Single Dimensional Array
  2. Multidimensional Array

Single Dimensional Array in java

Syntax to Declare an Array in java

Flash Soon


Instantiation of an Array in java

Flash Soon


Example of single dimensional java array

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

Flash Soon


Output:

Declaration, Instantiation and Initialization of Java Array

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

Flash Soon


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

Flash Soon


Output:

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 minimum number of an array using method.

Flash Soon


Output:

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

Flash Soon


Example to instantiate Multidimensional Array in java

Flash Soon


Example to initialize Multidimensional Array in java

Flash Soon


Example of Multidimensional java array

Example of Multidimensional java array

Flash Soon


Output:

What is the class name of java array?

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

Flash Soon


Output:

Copying a java array

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

Syntax of arraycopy method

Flash Soon


Example of arraycopy method

Flash Soon


Output:

Addition of 2 matrices in java

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

Flash Soon


Output:

Copyright ©2017 Design& development