Arrays:
Array is the type of Linear data structures.
• Definition:
Normally, an array is a collection of similar type of data. Elements of an array have the contiguous (Adjacent) memory location.
Java array is an object containing elements have similar data types.
Each element in an array is assigned with the unique index or a key starting from zero and increase sequentially. The index is used to access and modify the elements in the array.
• Creating Array in Java:
To create an array in Java, you must specify the type of elements that the array will hold, as well as its size. The syntax to create an array is as follows:
dataType[]arrayName=new dataType[arraySize];
Here, dataType represents the data type of the array elements, arrayName is the name of the array, and arraySize is the number of elements that the array can hold.
For example, to create an array of integers that can hold five elements, you can use the following code:
int[] myArray = new int[5];
Here, int is the data type of an array and 5 is the size of an array (how many elements does an array contains).
This was the preferred way of creating an array in java. But you may have another way of creating an array in java that is referred as inappropriate way of creating array in java as we create in C/C++.
dataType[] arrayName = { //Elements of an array separated by commas " , " };
This was the similar way of creating an array in java as we create in C/C++.
• Initializing an array elements:
We can initialize the array elements by directly accessing them with their index. For example for the array of size 5 created above we can initialize elements of array as:
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
As you can see we have the maximum index for an array of size 5 is 4, An array of size N have the maximum index (N-1) and the last element is at the last index (as given in the picture above the article).
This was the way of initializing when we know the values (program is hard coded). What if when we have to take input for the every element of an array from the user?
For that purpose we can use a loop (for loop) for taking the inputs for the array elements from the user:
Scanner input = new Scanner(System.in);
for(int i = 0; i < myArray.length; i++) {
myArray[i] = input.nextInt();
}
"Scanner class's object is used in java to take input and for taking integer input we call nextInt() method with Scanner object."
• Printing an Array:
You can also use loops to iterate over the elements in an array, and perform operations on them. For example, to print all the elements in an array, you can use the following code:
for(int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
• Types of Array:
In Java, there are several types of arrays that you can use depending on your requirements. Here are some of the most common types of arrays in Java:
1. One-dimensional array: A one-dimensional array is the simplest type of array in Java. It consists of a single row of elements, all of the same data type.
2. Two-dimensional array: A two-dimensional array is an array of arrays. It consists of rows and columns, and each element in the array is accessed using two indices. A two-dimensional array is useful when working with matrices and other multi-dimensional data structures.
3. Three-dimensional array: A three-dimensional array is an array of arrays of arrays. It consists of multiple rows, columns, and layers, and each element in the array is accessed using three indices. Three-dimensional arrays are useful when working with complex data structures that require more than two dimensions.
4. Jagged array: A jagged array is an array of arrays, where each row can have a different number of elements. Jagged arrays are useful when working with data structures where the size of each row is not fixed.
5. Object array: An object array is an array of objects, where each element in the array is an object of a particular class. Object arrays are useful when working with collections of objects, such as a collection of employees or customers.
6. Primitive array: A primitive array is an array of primitive data types, such as int, char, or boolean. Primitive arrays are useful when working with large collections of data that do not require the use of objects.
• Uses of an Array:
Arrays are commonly used in Java programs to store collections of data, such as lists of numbers or strings. They are also used in algorithms and data structures, such as sorting and searching. In addition, Java provides several methods to manipulate arrays, such as sort() and copyOf().
Overall, arrays are a powerful and versatile data structure in Java, and they are used extensively in a wide range of Java programming tasks.
0 Comments