Java is one of the most popular computer programming languages in the world, Java is an essential part of any web and application development professional's toolkit. to understand this powerful language there are many component and concepts, in this blog, we are going to discuss about arrays in Java Programming. Whether you are an experienced programmer or going to start your programmer career, you must have to use arrays in almost all aspects of Java programming.
What is an Array in Java?
An array represent a data structure that contains homogeneous/similar elements. This means in the array all the elements are of the same data types. Let's see the example:
given above, this is an array of seven elements. All the elements are integers and homogeneous (same data type). the given number which are given below starts from 0-6 is called index of the array, which always starts from 0 and goes up to n-1 elements. in this example, there are seven elements, so the index is from zero to six. There are three main features of an array:
- Dynamic allocation: memory in the array is created dynamically, which minimize the amount of storage required for the program.
- Elements stored under a single name: All the elements are stored under one name, which mean this name will be used anytime to call the element of that array.
- Occupies contiguous location: Elements in the arrays are placed at adjacent positions. This makes it easy for the user to find the locations of its elements.
Advantages of Arrays in Java
- we can access any element in our java arrays with the help of indexes.
- It is easy to store and manipulate/used large data in arrays.
Disadvantages of Arrays in Java
- Arrays have a fixed size,, that's why we can not change the size of the array once it is declared
- Java cannot store heterogeneous (different type) data. It can only store a single type (same type) of primitives
After understanding what Java arrays are, let us look at how arrays in Java are declared and defined.
Define an Array in Java
Java is easy to define and declare arrays. First, we have to define the array, the syntax to define array is:
Here, the type is String, int, double, or long. Var-name is the name of the array variable.
Declare an Array in Java
These are two ways that you can declare an array in Java. You can assign values to elements of array like this:
We have declared an array arr of type integer, its size is 5. The array is assigned with elements for each of the index positions. We will run a for loop to print all 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 the above program, output that you will get using this array is as follows:
Now you know how to create/make and use an array in Java, let's look at the types of arrays.
Types of Arrays
Arrays has three types, we use these types as per the requirement of the program. These are:
1. One-dimensional Array
It is also known as a linear array, the elements are stored in a single row. For example:
In the above given example, we have an array containing five elements. They are kept in a single line or adjacent memory locations.
Look at this example in Java source code. Here, five elements are 1, 2, 3, 4, and 5. We have used a for loop to print the elements of the array. The output of this is as follows:
2. Two-dimensional Array
Two dimensional array store the data in 2 portions, one represent rows and the second represent columns:
In this, the integer array has two rows and five columns. The index begin from 0,0 in the left-upper corner to 1,4 in the right lower corner.
In this Java source code, we have a two-dimensional array. We have 2 rows and 3 columns. Brackets ([]) separate the rows, and the number of elements separates the columns. For this, we use two (2) for loops: one for rows and the other for columns. When we run this program, the result will be as follows:
3. Multi-dimensional Array
The combination of two or more arrays or nested arrays is known as multi dimensional array. Even, we can use more than two rows and columns using the below code:
We are using here three rows and three columns, but using only two for loops. No matter how many rows and columns are entered, the number of for loops will always be two.
Now we know somethings about the types of arrays, let us look at some examples here.
Addition using Java Arrays
Java code to find the sum of all the elements in an array.
The above source code will add all the elements defined in my_array[] and produce/store the result in sum variable.
Multiplication using Java Arrays
Java code to find the sum of all the elements in an array.
This program will multiply all the elements defined in my_array[] and produce/store the result in mul variable.
Copying using Java Arrays
We can copy one array to another by using/calling Arrays.copyOf() method.
The above piece of code will store all the elements of the array "a" in the newly created array "b".
Cloning using Java Arrays
Java supports/allowed object cloning with the help of the clone() method to create same copy of an object.
Binary Search Using Java Arrays
Learning binary search algorithms is recommended for a better understanding of arrays. All sorting and searching algorithms start with arrays. We use binary search to find the element in an array. It divides the array into two same parts, and the elements in the array must be in ascending order. Then, compare the value of the element and the middle element of the array. 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 start in the lower half with the same process.
For example:
We have an array of 7 elements; suppose we have to find the element 11. The algorithm is applied, and by index number we get the element.
The following is a Java source code for binary search:
The following is the code for the main() program.
In this program elements of the array gets from the user and then asks the user himself/herself to find the element in the array. output of this program will be:
Firstly, enter the elements in ascending order. Next, we have to put the element we want to search for and the result will be as given:
0 Comments