Getting Started with Arrays in Java

Building for the now and the future.
Arrays are one of the most important data structures in Java. They are used to store a collection of data of the same type. Arrays are objects in Java, and they are declared using the [] syntax.
For example, the following code declares an array of integers:
int[] array = new Int[10]
// This creates an array of 10 integers
This creates an array of 10 integers. The elements of the array can be accessed using their index. The index of the first element is 0, the index of the second element is 1, and so on.
Note on Indexing
Indexing in programming differs from indexing in mathematics, in Mathematics we start indexing from 1, but in Programming it is entirely different as Indexing starts from 0. So in arrays, the indexing goes in this range 0 - (n-1). Where n stands for the number of elements in the array
We can get the values of different elements from the array by passing the correct index number
Here is an example to show that:
System.out.println(array[0]);
// This prints out the first element of the array
System.out.println(array[5]);
// This prints out the sixth element of the array
System.out.println(myArray[9]);
// This prints out the last element of the array
Arrays can be initialized when they are declared. For example, the following code declares and initializes an array of 5 integers:
int[] array = {1, 2, 3, 4, 5};
Arrays can be used to perform a variety of tasks, such as storing data, sorting data, and searching for data.
Common Array Methods in Java
1. sort(): sorting an array
int[] ages_of_students = {23, 22, 21, 24, 28, 27};
Arrays.sort(ages_of_students); // Sorts the specified array of objects into ascending order, according to the natural ordering of its elements
System.out.println(ages_of_students);
2. sort(Collections, reverseOrder());
String[] strings = {"Fisayo", "Kanye", "Tony", "Groin", "Help"};
Arrays.sort(strings, Collections.reverseOrder()); // Returns a comparator that imposes the reverse of the natural ordering on a collection of objects
for (String str : strings) {
System.out.println(str)
} // this prints out the reversed order of the array
3. toString()
char[] charray = {'H', 'e', 'l', 'l', 'o'};
String str = Arrays.toString(charray); // Returns a string representation of the contents of the specified array
System.out.println(str); // this prints out the array as a single String
4. asList()
// Create an array.
int[] myArray = {1, 2, 3, 4, 5};
// Create a List object from the array.
List<Integer> myList = Arrays.asList(myArray); // returns a fixed-size list backed by the specified array.
myList.add(6); // Add an element to the List object.
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i));
} // Print the elements of the List object.
5. binarySearch()
Binary search is a search algorithm that works on sorted arrays. It works by repeatedly dividing the array in half and comparing the target value to the middle element of the array. If the target value is equal to the middle element, the search is successful and the index of the middle element is returned. If the target value is less than the middle element, the search is continued on the lower half of the array. If the target value is greater than the middle element, the search is continued on the upper half of the array. This process is repeated until the target value is found or the entire array has been searched.
Here is an example of how binary search can be used to find the value 5 in the array {1, 3, 5, 7, 9}:
// Create an array.
int[] myArray = {1, 3, 5, 7, 9};
// Set the low and high indexes to the beginning and end of the array.
int low = 0;
int high = myArray.length - 1;
// Set the target value.
int target = 5;
// While the low index is less than or equal to the high index.
while (low <= high) {
// Calculate the middle index.
int mid = (low + high) / 2;
// Compare the target value to the middle element.
if (target == myArray[mid]) {
// The target value has been found.
System.out.println("The target value is at index " + mid);
break;
} else if (target < myArray[mid]) {
// The target value is less than the middle element.
high = mid - 1;
} else {
// The target value is greater than the middle element.
low = mid + 1;
}
}
This code will print the following output:
The target value is at index 2
// recall the rule on indexing
Advantages and Disadvantages
There are a number of advantages to using arrays in Java. Some of these advantages include:
Arrays are efficient. Arrays can be accessed quickly and easily, which makes them ideal for storing large amounts of data.
Arrays are easy to use. Arrays are easy to declare and initialize, and they can be used to perform a variety of tasks.
Arrays are flexible. Arrays can be resized, which makes them ideal for storing data of varying sizes.
There are also a number of disadvantages to using arrays in Java. Some of these disadvantages include:
Arrays are fixed size. Once an array is created, its size cannot be changed. This can be a problem if you need to store data of varying sizes.
Arrays are not thread-safe. Arrays cannot be accessed by multiple threads at the same time. This can lead to race conditions and other problems.
Arrays are not garbage collected. Arrays are not garbage collected by the Java virtual machine. This means that you must explicitly delete arrays when you are finished with them.
Conclusion
Arrays are a powerful tool that can be used to store and manipulate data in Java. However, it is important to be aware of the advantages and disadvantages of arrays before using them.
