top
upGrad KnowledgeHut SkillFest Sale!

Search

Java Tutorial

Data structure: ArrayA data structure is a specific way in which information can be efficiently organized in a computer. For example, using the array data structure, we can store a list of items with the same data type. An array is normally a set of comparable types of elements with a contiguous place of memory. Java array is an object with comparable information type elements. It is a structure of information where comparable elements are stored. Only a fixed set of elements can be stored in a Java array. Java array is index-based, the array's first elements are placed at index 0. Advantages of array in java Code Optimization: It optimizes the code; we can effectively retrieve or sort the information. Random access: We can get any information in an index place. Disadvantages of an array in Java Size Limit: Only the fixed size of elements can be stored in the array. At runtime, it does not increase its size. In Java, which develops automatically, the collection framework is used to fix this issue. Below is the way we can declare an array in Java: Type[] name; name is an array name or identifier.  type can be any data type like byte, short, char, int, etc. or an object. Let's see the above example one more time. int[] salary; Here, salary is an array that can hold values of type int. however, how many elements can array hold? We haven't defined it yet. The next step is to allocate memory for array elements. data = new int[5]; The length of the salary array is 5. Meaning, it can hold 5 elements (5 int values in this case). Remember, once the array length is defined, the program cannot change it. Java Array Index  Using indices, you can access components of an array. Let's take an instance below  short[] age = new short[5]; The first element of an array is age[0], the second is age[1] and so on. If the length of an array is n, the last element will be arrayName[n-1]. Since the length of the age array is 5, the last element of the array is age[4] in the above example. For numeric kinds, the default original value of array components is 0 and for boolean false. In Java, during declaration, you can initialize arrays or you can initialize (or alter values) later in the program according to your need. Initialize an Array During Declaration Below is an example of how we can initialize an array during declaration in java program. int[] age = {12, 4, 5, 2, 5}; This statement creates an array and initializes it during the declaration. The length of the array is determined by the number of values provided which is separated by commas. In our example, the length of the age array is 5. Let's write an easy array element printing program. class MyArrayProgram {  public static void main(String[] args) {  int[] age = {123, 441, 50, 21, 50};  for (int i = 0; i < 5; ++i) {  System.out.println("Value at index " + i +": " + age[i]);        }     }  } When you run the program, the output will be: Value at index 0: 123  Value at index 1: 441  Value at index 2: 50  Value at index 3: 21  Value at index 4: 50 By using its numeric index, we can readily access and modify array components. There are two types of an array in java. Single Dimensional Array Multidimensional Array Single Dimensional Array in Java Syntax to Declare an Array in Java dataType []arr; (or)  dataType[] arr; (or)  dataType arr[];  Instantiation of an Array in Java arrayRefVar=new datatype[size];  Multidimensional Array in Java In such a case, data is stored in a row and column-based index (also known as matrix form). Syntax to Declare Multidimensional Array in Java dataType arrayRefVar[][];  dataType []arrayRefVar[];  dataType[][] arrayRefVar;  dataType [][]arrayRefVar;  Example to instantiate Multidimensional Array in Java int[][] arr=new int[3][3]; //3 row and 3 column  ArrayIndexOutOfBoundsException in java The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if the array length is negative, equal to or higher than the array size while passing through the array. // Java Program to demonstrate the case of  ArrayIndexOutOfBoundsException in a Java Array.    public class CheckArrayException{    public static void main(String args[]){    int arr[]={550,6,770,8};    for(int i=0;i<=arr.length;i++){    System.out.println(arr[i]);    }    }} Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4  at CheckArrayException.main(TestArrayException.java:5)  550  6  770  8
logo

Java Tutorial

Data Structure:Array

Data structure: Array

A data structure is a specific way in which information can be efficiently organized in a computer. 

For example, using the array data structure, we can store a list of items with the same data type. 

An array is normally a set of comparable types of elements with a contiguous place of memory. 

Java array is an object with comparable information type elements. It is a structure of information where comparable elements are stored. Only a fixed set of elements can be stored in a Java array. 

Java array is index-based, the array's first elements are placed at index 0. 

Advantages of array in java 

  • Code Optimization:  It optimizes the code; we can effectively retrieve or sort the information. 
  • Random access:  We can get any information in an index place. 

Disadvantages of an array in Java 

  • Size Limit: Only the fixed size of elements can be stored in the array. At runtime, it does not increase its size. In Java, which develops automatically, the collection framework is used to fix this issue. 

Below is the way we can declare an array in Java: 

Type[] name; 

  • name is an array name or identifier.  
  • type can be any data type like byte, short, char, int, etc. or an object. 

Let's see the above example one more time. 

int[] salary; 

Here, salary is an array that can hold values of type int. 

however, how many elements can array hold? 

We haven't defined it yet. The next step is to allocate memory for array elements. 

data = new int[5]; 

The length of the salary array is 5. Meaning, it can hold 5 elements (5 int values in this case). 

Remember, once the array length is defined, the program cannot change it. 

Java Array Index  

Using indices, you can access components of an array. Let's take an instance below  

short[] age = new short[5]; 

Java Array Index 

The first element of an array is age[0], the second is age[1] and so on. 

If the length of an array is n, the last element will be arrayName[n-1]. Since the length of the age array is 5, the last element of the array is age[4] in the above example. 

For numeric kinds, the default original value of array components is 0 and for boolean false. 

In Java, during declaration, you can initialize arrays or you can initialize (or alter values) later in the program according to your need. 

Initialize an Array During Declaration 

Below is an example of how we can initialize an array during declaration in java program. 

int[] age = {12, 4, 5, 2, 5}; 

This statement creates an array and initializes it during the declaration. 

The length of the array is determined by the number of values provided which is separated by commas. In our example, the length of the age array is 5. 

Initialize an Array During Declaration

Let's write an easy array element printing program. 

class MyArrayProgram public static void main(String[] args) { 
int[] age = {123, 441, 50, 21, 50}; 
for (int i = 0; i < 5; ++i) { 
System.out.println("Value at index " + i +": " + age[i]); 
      } 
   } 
} 

When you run the program, the output will be: 

Value at index 0: 123 
Value at index 1: 441 
Value at index 2: 50 
Value at index 3: 21 
Value at index 4: 50 

By using its numeric index, we can readily access and modify array components. 

There are two types of an array in java. 

  • Single Dimensional Array 
  • Multidimensional Array 

Single Dimensional Array in Java 

Syntax to Declare an Array in Java 

  • dataType []arr; (or)   
  • dataType[] arr; (or)   
  • dataType arr[];   

Instantiation of an Array in Java 

  • arrayRefVar=new datatype[size];   

Multidimensional Array in Java 

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

Syntax to Declare Multidimensional Array in Java 

  • dataType arrayRefVar[][];  
  • dataType []arrayRefVar[];    
  • dataType[][] arrayRefVar;  
  • dataType [][]arrayRefVar;  

Example to instantiate Multidimensional Array in Java 

  • int[][] arr=new int[3][3]; //3 row and 3 column   

ArrayIndexOutOfBoundsException in java 

The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if the array length is negative, equal to or higher than the array size while passing through the array. 

// Java Program to demonstrate the case of  ArrayIndexOutOfBoundsException in a Java Array.   
public class CheckArrayException{   
public static void main(String args[]){   
int arr[]={550,6,770,8};   
for(int i=0;i<=arr.length;i++){   
System.out.println(arr[i]);   
}   
}} 

Output: 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 
at CheckArrayException.main(TestArrayException.java:5) 
550 
6 
770 
8 

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!