Introduction To An Array In Java

Use of an Array in Java

 
An 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 index.
 
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 is 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 array.
  • Single Dimensional array
  • Multidimensional array
Single Dimensional array in Java. 
 
Step 1
 
Let's open Notepad and write the code, given below.
  1. class demo {  
  2.  public static void main(String arg[]) {  
  3.   char arr[] = {  
  4.    30,  
  5.    20,  
  6.    40,  
  7.    50,  
  8.    10  
  9.   };  
  10.   for (int x: arr) {  
  11.    System.out.println(x);  
  12.   }  
  13.  }  
  14. }   
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 that is written in my arry.java file.  
Output
 
java
 
Step 7
 
Now, code for an array.
  1. //Variable length argement(….a)  
  2. class demo {  
  3.  void show(int… a) {  
  4.   for (int z: a) {  
  5.    System.out.println(z);  
  6.   }  
  7.  }  
  8.  public static void main(String arg[]) {  
  9.   demo d = new demo();  
  10.   d.show(102030405060708090100);  
  11.  }  
  12. }     
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 that is written in my arr.java file.  
Output
 
java
 

Multidimensional array in Java

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