technology

Introduction to the Concept of Arrays in Java - What it is, How to use it, and More!

In order to learn a programming language and write code in it, it is important to understand its fundamental concepts. Arrays in Java are a similar concept that is essential to learn.

arrays in java

Arrays in Java are amongst the most fundamental concepts that programmers need to learn. It is essential because almost every programmer needs to get a hold of it. It is because arrays have a much wider application than one can anticipate. Therefore, it becomes essential to learn the concept of arrays.

In this article, we have tried to provide a basic understanding to develop your own array program in Java.

What are Arrays in Java?

To simply define arrays in Java, an array is a single variable declared in Java that holds multiple values.

For instance, a single variable holding a value in Java would look like this:

int num = 23;

However to define an array in Java or declare a new array in Java, it would look something like this:

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

This is another example for storing words in java programs on strings and arrays:

string Cars[ ]= {Lamborghini, Ferrari, Mercedes, Mustang, Volkswagen};

The idea behind arrays in Java is to create datasets of similar datatype and with similar metadata. 

Types of Array in Java

There are several types of arrays in Java that can be used for holding values that are numeral, alphabetical, and decimal in nature. To do that there are multiple datatypes that can be used to initialize the data in a variable.

Note: For ease of understanding and visualizing the array, we’ll use the example X, Y, and Z axis.

The different types of arrays in Java are:

1. One Dimensional Array (1-D)

A one-dimensional array is linear in nature. It means the data stored in a one-dimensional array lies on a single axis. In this case, you can imagine that the data is stored on X-axis.

An example of one-dimensional array would be:

int arr[ ]= {2,4,3,6,5,7,};

2. Two Dimensional Array (2-D)

A 2 dimensional array in Java lies on two axis. For ease of understanding, consider them X and Y. Another way of understanding 2 dimensional arrays in Java is to understand them as data declared in the form of rows and columns. 

Here is a two dimensional array in Java with example:

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

3. Three Dimensional Array (3-D)

A three dimensional array will be laid on 3 axis i.e. X, Y, and Z. This can be divided into blocks, rows, and columns. A three dimensional if you can imagine looks like a 3D cube.

Example of three dimensional array would be:

int num [ ][ ][ ] = {{{1,2,3}}, {{1,2,3}}, {{2,3,1}},{{1,2,3}}, {{1,2,3}},{{2,3,1}}, {{1,2,3}}, {{1,2,3}}, {{2,3,1}}};

How does an Array Program in Java look like?

Here’s an example of how an array program in Java look like:

new array in java

The example above prints the integer stored in the array. 

Array Declaration in Java

Considering that there are three types of arrays in Java. Below are the ways you can declare an array in Java.

1. One Dimensional Array:

The array declaration in Java for a one dimensional array would look like:

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

A) int:

Here “int” is used for declaring the datatype. For instance, if we use “string” in place of “int” then the data inside the array would be considered as a string. The value of a string is pre-established based on ASCII (American Standard Code for Information Interchange) codes. If two strings are compared then the ASCII value of each string is compared with the other one. It is possible because in the case of a string, the string used as the value has its own index, for example, “words”. The index value of “w” in “words” would be 0 while the value of “s” would be “4”. 

Note: The count for the index starts from 0 in every case.

B) num[5]:

“Num” is the name of the variable through which this array would be identified. The number “5” inside the square bracket describes the length of the array. If we are providing the value of the array in the program itself, there is no explicit need for describing the length beforehand. However, if you are taking input from the user, describing the value beforehand would be ideal.

C) {1,2,3,4,5}:

This is the data inside the array that has been input into the array beforehand in the program. In order to take the input from the user, the programmer is required to use the input function along with a loop.

2. Two Dimensional Array:

The declaration of 2 dimensional array in Java would look like this:

Int num[5 ] [5 ]={{1,2,3,4,5},{6,7,8,9,10}}

num[5][5]:

Here the digits “5” and “5” is the length of the rows and column. Visually, the data stored inside the array would look like the table below:

array declaration in java

These values are accessed in an array using their index values. For instance, the index value of the data “1” would “0,1”, the index value of the data “5” would be “4,4”, the index value of “8” would be “3,2”

3. Three Dimensional Array:

Below is the example of three dimensional array declaration in Java:

Int num [3 ][ 3][3 ] = {{{1,2,3}}, {{1,2,3}}, {{2,3,1}},{{1,2,3}}, {{1,2,3}},{{2,3,1}}, {{1,2,3}}, {{1,2,3}}, {{2,3,1}}};

num[3][3][3]:

Here the value inside the square brackets “3”,”3”, and “3” describes the block, rows, and columns in the same order as stated here.

Note: The visualization of a 3D array in a 2D space would look similar to a 2 dimensional array in Java. However, it is not possible to showcase how the values are stored in three dimensional array in Java.

Significance of Array Data Structure in Java

To understand the significance of an array and the relation between an array & data structure, it is important to understand what data structure actually means.

What is Datastructure?

A data structure in essence is a format for storing data. The idea behind forming a data structure is to organize, process, store, and retrieve it with ease. Every data structure follows a logic and provides the capability to store and use that data with that logic. One of the most common examples of it if you are aware would be stack and queue.

1 3 4 4 7 6 2 8 1 0 9

Now, if we have an array then one can understand that it is in itself a linear data structure. An array in essence is nothing but a way to store similar data using a single variable used for its identification. Another interesting aspect is that the datatype remains the same for each array and the variable using which it is initialized. For instance, int stands for integer, string is used for both alphabets and digits, boolean is for binary, etc.

How to Reverse an Array in Java?

There are several methods to reverse an array in Java. It is important because many times, it is required to access the data from its last index. When these concepts are applied on an advanced level, use cases such as this help in sorting, searching, and finding the data with ease.

1st Method: Printing in the Reverse Order

In this method to reverse an array in Java, we use the length property to access all the indexes in the array to access all the data stored in it. After that, we run a reverse loop from the last index and print the result out.

array syntax in java

2nd Method: Reversing an array and ArrayList in Java

In this, we are creating a reverse function explicitly. The logic in this function is created such that once an array is passed on to it, it reverses the values of the array and returns the value to the code when it was called.

array declaration in java

3rd Method: Utilizing the loop for reversing the array

In this method similar to the previous one, we create a function name “reversed”. In the main method, we declare an array with the value. After that, we calculate the length by using the length attribute and then call the function. 

In this function, we declare a new array and run a loop in reverse to store the values in the original array in reverse order. Once that is laid out, we print the values using another “for” loop.
 
how to implement array in java

Wrapping Up!

Java is one of the most popular and widely used languages in the world. In fact, even the top software development companies in USA use it for a lot of their projects. Learning arrays in Java is almost fundamental to utilizing the programming language to its full potential. It is important to understand because, during advanced applications, enormous streams of data are stored inside it. As a programming student, it is essential to understand the applications of arrays and how they can be utilized well. With this article, our intention was to provide you with a basic understanding to learn arrays in Java. We hope this article may have been of some help to you. Also thank you for reading it until the end.

General FAQ

  • What is an array in Java?
  • Who would win between Kotlin vs Java?
  • How to learn array in Java?
  • What are some resources for learning Java array tutorial with examples?
  • What is a multidimensional array in Java?
Aparna <span>Growth Strategist</span>
Written By
Aparna Growth Strategist

Aparna is a growth specialist with handsful knowledge in business development. She values marketing as key a driver for sales, keeping up with the latest in the Mobile App industry. Her getting things done attitude makes her a magnet for the trickiest of tasks. In free times, which are few and far between, you can catch up with her at a game of Fussball.

Want To Hire The Best Service Provider?
MobileAppDaily will help you explore the best service providers depending on your vision, budget, project requirements and industry. Get in touch and create a list of best-suited companies for your needs.

Featured Success Stories

technology

Internet of Things (IoT) & Its Impact On App Development

4 min read  

Internet of Things in today's world is taking the meaning of connectivity to a whole new level. And this includes the integration of IoT in various industry verticals, one of them being app development. So if you want to get a better understanding of how IoT impacts mobile app development, keep

technology

How To Download And Use ACMarket App Store

4 min read  

The Google Play Store has over 2.5 million apps, still, there are many apps that don’t pass through the stringent Play Store’s guidelines and end up in some of the other third-party app stores.Thankfully, there is now an alternative to the official Android app store and its called AC

technology

Top Android Features That Are Inspired by Apple

4 min read  

Android and iOS have always been arch rivals of each other, and are in a constant race to become the most preferred operating systems by a large share of smartphone users in the world. Having said that, Android is currently gaining over iOS in the global smartphone market by a decent margin and the

technology

The World Of Augmented Reality Technology [Complete Guide]

4 min read  

Nowadays, we have seen a lot of involvement with the Augmented Reality technology in our lives. There's no doubt that this AR technology has been contributing a lot in implementing simple tasks as well as the complex ones like adding animation in social media filters and assisting in performing

MAD Originals
MAD Originals

Cut to the chase content that’s credible, insightful & actionable.

Get the latest mashup of the App Industry Exclusively Inboxed

  • PRODUCTS
  • SERVICES
  • BOTH
Join our expansive network, build connections and expand your brand presence.