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 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.
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.
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:
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,};
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}};
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}}};
Here’s an example of how an array program in Java look like:
The example above prints the integer stored in the array.
Considering that there are three types of arrays in Java. Below are the ways you can declare an array in Java.
The array declaration in Java for a one dimensional array would look like:
int num[5]={1,2,3,4,5};
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.
“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.
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.
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:
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”
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.
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.
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.
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.
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.
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.
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.
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.
Cut to the chase content that’s credible, insightful & actionable.
Get the latest mashup of the App Industry Exclusively Inboxed