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.
Below is the way we can declare an array in Java:
Type[] name;
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.
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.
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 in Java
Syntax to Declare an Array in Java
Instantiation of an Array in Java
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
Example to instantiate Multidimensional Array in Java
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
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *