Arrays In Java: Declare, Define, and Access Array

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

We'll cover the following topics Java arrays article:

  1. What is an array in Java?
  2. How to define and declare an array in Java
  3. Types of arrays
  4. Example of Java arrays

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:

array-java.

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: 

  1. Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount of storage required for the code. 
  2. Elements stored under a single name: All the elements are stored under one name. This name is used any time we use an array. 
  3. 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. 

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

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: 

var-name

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:

public-class

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.  

 public-static-void

After executing this program, the output that you will get using this array is as follows:

element

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: 

int array

In this example, we have an array of five elements. They are stored in a single line or adjacent memory locations.

/public-class-main

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: 

sum

2. Two-dimensional Array

Two-dimensional arrays store the data in rows and columns:

2darray

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: 

public-class-2main

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  */

/addition-1

The above program will add all the elements defined in my_array[] and produce the result.

addition2

Multiplication using Java Arrays

/* Java Program to find the sum of all the elements in an array  */

multiplication.

The above program will multiply all the elements defined in my_array[] and produce the result.

multiplication-2

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

cloning1.

cloning2

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. 

For example: 

binary-search

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:

binary-search-code

This is the code for the binary search class. 

The following is the code for the main() program.

elements-array

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: 

output

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: 

search-res

Conclusion

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? 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.