One of the most popular programming languages in the world, Java is an essential part of any web and application development professional's toolkit. While there are many components and concepts to understand this powerful language, in this article, we are going to talk about arrays in Java. Arrays are a straightforward yet essential concept of Java programming. Whether you are an experienced programmer or a beginner, you will inevitably use arrays in almost all aspects of Java programming.
What is an Array in Java?
An array refers to a data structure that contains homogeneous elements. This means that all the elements in the array are of the same data type. Let's take an example:
This is an array of seven elements. All the elements are integers and homogeneous. The green box below the array is called the index, which always starts from zero and goes up to n-1 elements. In this case, as there are seven elements, the index is from zero to six. There are three main features of an array:
- Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount of storage required for the code.
- Elements stored under a single name: All the elements are stored under one name. This name is used any time we use an array.
- Occupies contiguous location: The elements in the arrays are stored at adjacent positions. This makes it easy for the user to find the locations of its elements.
Advantages of Arrays in Java
- Java arrays enable you to access any element randomly with the help of indexes
- It is easy to store and manipulate large data sets
Disadvantages of Arrays in Java
- The size of the array cannot be increased or decreased once it is declared—arrays have a fixed size
- Java cannot store heterogeneous data. It can only store a single type of primitives
Now that we understand what Java arrays are- let us look at how arrays in Java are declared and defined.
Define an Array in Java
Arrays in Java are easy to define and declare. First, we have to define the array. The syntax for it is:
Here, the type is int, String, double, or long. Var-name is the variable name of the array.
Declare an Array in Java
These are the two ways that you declare an array in Java. You can assign values to elements of the array like this:
We have declared an array arr of type integer. The size of the array is 5, meaning that it can have five elements. The array is assigned with elements for each of the index positions. We'll run a for loop to print the elements in the array. A counter variable "i" is used to increment the index position after checking if the current index position is less than the length of the array.
After executing this program, the output that you will get using this array is as follows:
Now that you know how to create and use an array in Java, let's look at the types of arrays.
Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.
Types of Arrays
There are three types of arrays. We use these types of arrays as per the requirement of the program. These are:
1. One-dimensional Array
Also known as a linear array, the elements are stored in a single row. For example:
In this example, we have an array of five elements. They are stored in a single line or adjacent memory locations.
Look at this example in Java code. Here, the five elements are 1, 2, 3, 4, and 5. We use a for loop to print the elements of the array. The output of this is as follows:
2. Two-dimensional Array
Two-dimensional arrays store the data in rows and columns:
In this, the array has two rows and five columns. The index starts from 0,0 in the left-upper corner to 1,4 in the right lower corner.
In this Java code, we have a two-dimensional array. We have two rows and three columns. Brackets separate the rows, and the number of elements separates the columns. For this, we use two for loops: one for rows and one for each element in the row. When we execute this program, the result will be as follows:
3. Multi-dimensional Array
This is a combination of two or more arrays or nested arrays. We can even use more than two rows and columns using the following code:
Here, we are using three rows and three columns, but we are only using two for loops. Regardless of how many rows and columns are entered, the number of for loops will always be two.
Now that we know about the types of arrays, let us look at some examples below.
Addition using Java Arrays
/* Java Program to find the sum of all the elements in an array */
The above program will add all the elements defined in my_array and produce the result.
Multiplication using Java Arrays
/* Java Program to find the sum of all the elements in an array */
The above program will multiply all the elements defined in my_array and produce the result.
Copying using Java Arrays
You can copy one array to another by using Arrays.copyOf() method.
The above piece of code will store the elements of the array "a" in the newly created array "b".
Cloning using Java Arrays
Java supports object cloning with the help of the clone() method to create an exact copy of an object.
Binary Search Using Java Arrays
Learning binary search algorithms is recommended for a better understanding of arrays. All searching and sorting algorithms start with arrays. A binary search is an algorithm used to find the element in an array. It divides the array into half, and the elements in the array must be in ascending order. Then, the value of the element and the middle element of the array is compared. If the value of the element is smaller than the value of the mid element, the greater half of the array is neglected, and the search is conducted in the lower half with the same process.
We have an array of seven elements; suppose we have to find the element 11. The algorithm is applied, and we get the element with the index number.
The following is a Java code for binary search:
This is the code for the binary search class.
The following is the code for the main() program.
This program gets the elements of the array from the user and then asks the user to find the element in the array. The output of this program will be:
First, enter the elements in ascending order. Next, you have to put the element you want to search for and the result will be as follows:
After reading this article, you should understand the basics of arrays in Java. You learned how to declare arrays and how to assign values to them. Then you saw the three types of arrays with an example of each along with Java code. You looked at how to implement a program to add elements in an array, multiply the array elements, and how to copy and clone arrays. Finally, you got an idea to implement a binary search program that made the concept of array clear. To learn more about Java Arrays, watch this video.
Want to Kickstart Your Career in Java Development?
If you're ready to take your software development career to the next level, check out Simplilearn's Full Stack Java Developer training course. This comprehensive online Blended Learning program will teach you everything you need to know about the tools and techniques used in Java programming. Once you complete the mix of learning exercises, expert advice, and real-world industry projects, you will earn a certificate to help you land your dream job or get that promotion. What are you waiting for?