Introduction To An Array In Java

Use of an Array in Java

Array is a collection of similar types of elements. However big an array is, its elements are always stored in the contiguous memory locations. This is a very important point, which we will discuss in more detail later.

To begin with, like other variables, an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. In this, we are done with the statement.

An array is a container object, which holds a fixed number of values of a single type. It is also known as a sub-scripted variable. The length of an array is established when the array is created. After creation, its length is fixed.
"Array is homogenous, fixed-size sequenced collection of elements of the same data type which are allocated contiguously in memory".
 
Before using an array, its type and dimension must be declared. Each item in an array is called an element and each element is accessed by its numerical intex.
 
It is a data structure, where we store similar elements. We can store only the fixed set of elements in a Java array.
 
Memory Representation of Array
 
The array in memory locations are placed in contiguous memory cells. The size of these cells is dependent on the type of data in the memory. The pointer to the array points to individual memory locations.
 
When the elements are entered in the array then the pointer is on the first location and then moves subsequently. Similarly when the elements are extracted the pointer moves downwards.

Types of Array in Java

There are two types of an array.

  • Single Dimensional array
  • Multidimensional array

Single Dimensional array in Java.

Step 1

Lets open Notepad and write the code, given below.

  1. //Code  
  2. class demo {  
  3.     public static void main(String arg[]) {  
  4.         char arr[] = {  
  5.             30,  
  6.             20,  
  7.             40,  
  8.             50,  
  9.             10  
  10.         };  
  11.         for (int x: arr) {  
  12.             System.out.println(x);  
  13.         }  
  14.     }  
  15. }  
Step 2

Name it as “arry.java” and save the file on any location. I saved at “C:\kiran\program”.

Step 3

Open command prompt (Press Window + R, write cmd and hit OK).

Step 4

Set the path.

Step 5

Now, write the code, given below to check whether my Java file is compiling or not.

javac arry.java.

My Java program compiled successfully.

java

Step 6

Write the code, given below in command prompt. Press enter and see the output.
  1. java demo  
  2. // demo is a class name which is written in my arry.java file.  
Output

java

Step 7

Now, code for an array.
  1. Variable length argement(….a)  
  2.     //code  
  3. class demo {  
  4.     void show(int… a) {  
  5.         for (int z: a) {  
  6.             System.out.println(z);  
  7.         }  
  8.     }  
  9.     public static void main(String arg[]) {  
  10.         demo d = new demo();  
  11.         d.show(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);  
  12.     }  
  13. }  
Step 8

Now, write the code, given below to check whether my Java file is compiling or not.

javac arr.java.

java

My Java program compiled successfully.

Step 9

Write the code, given below in command prompt. Press enter and see the output.
  1. java demo  
  2. // demo is a class name which is written in my arr.java file.  
Output

java

Multidimensional array in Java

Step 10

Lets open Notepad and write the code, given below.
  1. //Code  
  2. class test {  
  3.     public static void main(String[] args) {  
  4.         int[][] values = new int[4][3];  
  5.         values[1][0] = 1;  
  6.         values[2][1] = 2;  
  7.         values[3][2] = 3;  
  8.         for (int i = 0; i < values.length; i++) {  
  9.             int[] total = values[i];  
  10.             for (int j = 0; j < total.length; j++) {  
  11.                 System.out.print(total[j] + ”“);  
  12.             }  
  13.             System.out.println();  
  14.         }  
  15.     }  
  16. }  
Step 11

Name it as “marray1.java” and save the file on any location. I saved at “C:\kiran\program”.

Step 12

Now, write the code, given below, to check whether my Java file is compiling or not.

javac marray1.java.

java

My Java program compiled successfully.

Step 13

Write the code, given below in command prompt. Press enter and see the output.
  1. java test  
  2. // demo is a class name which is written in my marray1.java file.  
Output

output

Happy coding. 
Ebook Download
View all
Learn
View all