News

What are the Arrays in Java?

Java is an Object Oriented Programming language developed by James Gosling at Sun Microsystems in 1995. It is a High-Level Language (HLL) and works on the principle of “Write Once, Run Anywhere.” Like any other programming language, Java too makes a lot of use of data and thus assigns different Data Structures to organize the data stored in memory. One such data structure is “Array”.

An array is a systematic organization of similar objects, usually in rows and columns. Array in Java is a data structure that stores different elements of homogeneous type. This means that all the elements present in an array are of the same data type (example: int, long, short, float, etc.).

The use of an array is that instead of declaring individual variables of the same data type again and again (like num0, num1, num2, num3,……) , you can simply declare an array of that particular data type and invoke its different indexes to access different numbers (like num[0], num[1], num[2], num[3],…….,num[n-1]). Here, num[ ] is the name of the array and ‘n’ is the number of elements.

Index in Array

An index is the prime locator of elements inside an array. Index is a part of contiguous location sequence, and by referencing it, all the elements of an array can be accessed. Index in the array always starts from 0 and goes up to n-1, where ‘n’ is the number of elements in the array.

So for example we have an array:

int arr[ ]={514,234,211,214,817,999,111,165},

then, the element ‘211’ can be accessed by index location 2, (i.e., arr[2]). This is shown with the help of the table below.

Index 0 1 2 3 4 5 6 7
Element 514 234 211 214 817 999 111 165

To better understand the concept of index, consider an array like a building which always starts from the Ground Floor and not the First Floor.

Key Features of Array in Java

Arrays are an essential part of the Java programming language as they simplify the process of accessing data, grouping data, and organizing them into sets.

  • Dynamic Memory Allocation: The biggest advantage of an array is that they allow dynamic memory allocation. This means that more memory is created as elements are added. This helps in reducing the memory load of the overall programming code.
  • Unified Name: Array groups all the elements together. Thus, they can be accessed by simply calling the array with reference to the index storing that particular number.
  • Fixed Size: Though the arrays in Java perform memory allocation during runtime, their size is however fixed. Once an array of a particular size (length) is declared, it cannot be changed.
  • Length: A unique feature of array (only in Java) is that we can access its length using the “length” member. We do not need to use a separate function, like sizeOf() in C++. The usage of this is shown below.

int size=arr[ ].length;

One important disadvantage of using arrays in Java is that you cannot group elements of different data types together. So if you have integer type numbers like 234, 412, and 321, and floating type numbers like, 333.21, 432.65, you cannot group them together in an array.

Types of Arrays

On the basis of data distribution, arrays are broadly classified into 2 categories:

  • 1-D array
  • 2-D array

One Dimensional (1-D) Arrays

1-D array is the term used for normally discussed arrays which have just 1 single row. An example is given below.

int arr[ ]= new int [10];

Here, an array of the name “arr” is being declared with a 1-D size of 10.

Note: Arrays in Java give you a bit of flexibility when it comes to syntax. So, writing any of the given 2 syntaxes is correct when declaring an array.

int num[ ] = new int [10];

int [ ] num= new int [10];

Two Dimensional (2-D) Arrays

2-D arrays are those which are arranged in both rows as well as columns. These are just like a 2-D matrix. While declaring 2-D arrays, there are 2 indexes, one pointing towards the row, and the 2nd pointing towards the element in that row.

A 2-D array can also be considered as an array consisting of references to other 1-D arrays. Declaration is displayed below.

int num[ ][ ]=new int [3][4];

or  int arr[ ][ ]={{1,2,3},{4,5,6},{7,8,9}};

2-D arrays are also known as Multidimensional arrays.

Jagged Arrays

Another kind of multidimensional arrays are jagged arrays. These are the ones where each row has a different number of columns. An example of a jagged array is:

3          4          5

7          1          6          9

2          5

To declare this Jagged Array in your code, follow the below syntax.

int arr[ ][ ]=new int[3][ ];

arr [0]=new int [3];

arr [1]=new int [4];

arr [2]=new int [2];

Operations on Arrays in Java

There are various operations which can be performed on arrays in Java. We have discussed some of the most common functions among these here below.

ArrayIndexOutOfBounds Exception

While compiling the code, the Java Virtual Machine (JVM) throws an exception (error) if the length of the array is negative, greater than, or equal to the size of the array.

This is because, for an array having ‘n’ elements, the index ranges from 0 to n-1. So if the index value given is less than 0 or greater than n-1, then the code will not be able to intercept it as that value doesn’t exist. Therefore, the JVM throws an error code in this case.

This error is given as:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

Searching in Arrays

Searching elements in an array is an essential and most common utility of the arrays. The different types of searching techniques in arrays in Java are:

  • Linear Search: In this method, every single element is picked and compared with the element being searched. The process continues until the required element is found. This process is time-consuming.
  • Binary Search: In this method, instead of picking every element and comparing, the middle element is checked whether it is smaller than or larger than the element being searched. This saves time. However, there is one prerequisite for Binary Search, that the array must be sorted.

Sorting in Arrays

Sorting is another common utility for arrays, through which arrays are arranged in proper ordered sequences, either ascending or descending. The top array sorting techniques in Java are:

  • Selection Sort
  • Bubble Sort
  • Insertion Sort
  • Merge Sort

The above mentioned operations are just a handful. In addition to these, there are multiple other operations and functions of arrays in Java like Cloning, Copying, Modifying, Passing to a Method, Returning Arrays from Methods, Declaring Arrays of Objects, etc.

It is time you download your local compilers and start running your own Array codes in Java now. Try looking for pattern programs from Google and use the concept of arrays to draw them. Hurry Up Now!!

Related Articles

Check Also
Close
Back to top button