Introduction
Java, one of the most widely used programming languages worldwide, is a crucial component of every professional web development toolset. Despite the fact that there are several elements and ideas to grasp when learning this robust language, this essay will focus on Java fields. A simple yet crucial Java programming concept is arrays. Whether you are an expert programmer or just starting out, arrays are a necessary component of practically all Java programming.
What is an array in Java?
An object with elements of a similar data type is called a Java array. In addition, each element of an array is kept in a single block of memory.The data structure is used to store comparable elements. In Java, an array can only contain a predetermined number of elements.
In Java, an array is based on an index. The array's initial element is kept at index 0, followed by the second member at index 1, and so forth.
An array is an object of a class that is dynamically created in Java. Java fields implement the Serializable and Cloneable interfaces and derive from the Object class. In Java, an array can hold either primitive values or objects.
Let's take an example:
There are seven elements in this array. Every element is homogenous and an integer. The index is the green box beneath the array, and it always begins at zero and goes up to n-1 elements. Since there are seven items in this instance, the index ranges from 0 to 6. Three main characteristics of an array are:
• Dynamic allocation: Because memory is produced dynamically in arrays, less storage space is needed for the code.
• Elements saved under one name: All elements are saved under one name. This name is used whenever we use the field.
• Occupies contiguous location: Elements in arrays are stored in contiguous positions. The user can easily find the location of its elements.
Our Learners Also Read: An Introduction to Thread in Java
How to initialize an array in Java
Initializing an array simply means assigning a value to the array. Let's initialize the fields we declared in the previous section:
String[] names = {"Jan", "Jade", "Love", "Allen"};
int[] myIntegers = {10, 11, 12};
We initialized our array by passing in values with the same data type, with each value separated by a comma.
Advantages of arrays in Java
• Java arrays allow you to access any element randomly using indexes.
• It is easy to store and manipulate large data files.
Disadvantages of arrays in Java
• After declaration, the size of an array cannot be increased or decreased - arrays are of fixed size.
• Java cannot store heterogeneous data. It can store only one type of primitive.
Now that we understand what Java arrays are, let's see its types, how arrays are declared and defined in Java.
Array types in Java
There are two types of fields.
• A one-dimensional array
• A multidimensional array
A one-dimensional array in Java
Syntax for declaring an array in Java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Array instantiation in Java
arrayRefVar=new DataType[size];
Java Array example
Let's look at a simple java array example where we will declare, instantiate, initialize and iterate over an array.
1. //Java Program to illustrate how to declare, instantiate, initialize
2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i
14. }}
Output:
10
20
70
40
50
Multidimensional Array in Java
In such cases, data is stored in a row and column-based index (also known as matrix form).
Syntax to Declare Multidimensional Array in Java
1. dataType[][] arrayRefVar; (or)
2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];
Example of instantiating Multidimensional Array in Java
1. int[][] arr=new int[3][3];//3 row and 3 column
Example to initialize Multidimensional Array in Java
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
Example of Multidimensional Java Array
Let's see the simple example: declare, instantiate, initialize and print the 2Dimensional array.
1. //Java Program to illustrate the use of a multidimensional array
2. class Testarray3{
3. public static void main(String args[]){
4. //declaring and initializing the 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6. //printing 2D array
7. for(int i=0;i<3;i++){
8. for(int j=0;j<3;j++){
9. System.out.print(arr[i][j]+" ");
10. }
11. System.out.println();
12. }
13. }}
Output:
1 2 3
2 4 5
4 4 5
How to Access Elements of an Array in Java?
An array's element can be accessed using the index number. The syntax for accessing an array's elements is shown below.
// access array elements
array[index]
Let's see an example of accessing array elements using index numbers.
Example: Access Array Elements
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
Output
Accessing Elements of Array:
First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5
Conclusion
You ought to be able to understand the fundamentals of Java arrays after reading this article. You gained knowledge of how to declare fields and how to give them values. You have now seen examples of each of the three types of arrays together with Java code.