Skip to main content

Arrays in Java

Published 3 min readMarkdown

An array is a collection of elements of the same data type, organized in a manner that allows them to be accessed via an index. Array elements are stored in contiguous memory locations, making it easy to access them in a sequential manner.

Arrays are basically of two types:

  1. 1-Dimensional Array

  2. 2-Dimensional Array

    In this blog we will cover 1-Dimensional Array only.

Syntax

An array is declared by specifying the data type of its elements, followed by square brackets [] to indicate that it is an array, and then the name of the array. The number of elements in the array can be specified by initializing it with a size value in square brackets [].

Here's an example of declaring an array in Java:

int[] arr = new int[5];

In this example, int is the data type of the elements in the array, followed by the name of the array arr, and then [] to indicate that it is an array. We initialize it with a size value of 5, so the array can hold five integer values. We can also give size by taking input from the user.

We can also initialize the elements of the array while declaring it:

int[] numbers = {1, 2, 3, 4, 5};

In this example, the array is declared with the same syntax as before, but the curly braces {} are used to specify the values of array elements.

Accessing Array Elements

Array elements can be accessed using the index . The index of the first element in every array starts from 0 and the last index is numbers.length-1, , numbers.length will give you the length of the array. Below is the example that how you can access a element of an array:

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // output: 1

We can also change the value of array elements:

int[] numbers = {1, 2, 3, 4, 5};
numbers[0] = 10;
System.out.println(numbers[0]); // output: 10

Common Array Operations

Iterating through an Array

Iterating over the elements of an array is a common operation in programming. There are two popular ways to iterate through an array in Java: using a for loop or using an enhanced for loop.

For Loop

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}
// here this for loop is used to print the elements of the array

Copy

Enhanced For Loop

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}

Sorting an Array

In java we can also sort the array by use sort(). This function sorts the elements in the array in ascending order.

int[] numbers = {5, 3, 1, 4, 2};
Arrays.sort(numbers);
for (int number : numbers) {
    System.out.println(number);
}

The output will be:

1
2
3
4
5

Searching an Array

Java provides an in-built binarySearch() method to search for an element in a sorted array.

int[] numbers = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(numbers, 3);
System.out.println(index); // output: 2

In this example, binarySearch() returns the index of the element 3, which is 2.

Conclusion

We can create an array of fixed length or dynamic length using the in-built data type. Arrays support different common operations such as iterating, sorting, and searching elements. The understanding of these operations is vital in programming as they help us manipulate array elements more efficiently. For more blogs on java or others languages stay tuned..

About the author

  • I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .

No comments yet. Be the first to comment.

More from this blog