Implementing Array in Java

Implementing Array in Java

4 mins read4.3K Views Comment
Updated on Mar 30, 2023 14:34 IST

Arrays in Java help to maintain elements of same datatype under a single variable name. The below article explains the implementation of array in java with examples.

2022_05_Arrays-in-Java-1.jpg

The article discusses the implementation of arrays in Java. It covers pros & cons, types, declarations, initialization, and accessing the elements of an array. Moreover, it goes through the method of assigning the reference of an array to another array.

Explore Java Courses

Contents

Let’s begin with an understanding of arrays in Java.

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

What is an Array?

The array is a data structure that is used to hold elements of the same type. For instance, an integer array will store only integer values, whereas a character array will store characters only. 

Array

Point to Remember about Arrays in Java:

  • An array is a linearly-ordered data structure that holds the same type of elements in contiguous memory addresses.
  • Array’s memory allocates at runtime, i.e., dynamic allocation.
  • In Java, Array treats as an object. Hence, the length property uses to find the length of the array.
  • Array index begins with 0 and ends at (length of array -1).
  • It can utilize as a static field, a local variable, or a method parameter.
  • The size of the array should either be an int or short.
  • An array can carry primitives variables (such as int, char, etc.) and object references of the class.
  • By default, the array initializes to 0 (zero) for numeric types, false for boolean, and null for reference types.

Note: Array in java is a reference type, not a value type. It holds an address rather than a value. Moreover, the size can vary at runtime as the memory allocates.

Example:


 
int Array_Name = new int[7];
Copy code
array_reference_variable

Pros and Cons of Using Array

Following are the advantages of using array in Java:

  • Using arrays increases code optimization.
  • Ordered and indexed data structure to store the same type of elements.
  • Elements of an array get stored in consecutive memory locations.
  • The array can store variables as well objects of a class.

Following are the disadvantages of using array in Java:

  • An array can only store the same type of elements.
  • Adding and removing elements in between is hard.

Types of Array in Java

Following are the types of arrays in Java:

types_of_array
  1. Single Dimensional Array – One-dimensional arrays are organized in a linear list in memory. Their indexes run from 0 total array size-1.
  1. Multidimensional Array – Arrays can be extended to any number of dimensions, such as two-dimensions, three-dimensions, etc. 

Moreover, a two-dimensional array is a type of multidimensional array. 2D arrays are similar to the 2D matrix. It consists of rows and columns. Here, each element in the array has a row and column index.

2d-array

How to Declare and Initialize an Array in Java?

Syntax of One-dimensional array in Java:

syntax-1d
syntax2-1d

Example 1:


 
//declaration
int iArray1[] = new int[4];
//initialization
iArray1[0] = 32;
iArray1[1] = 13;
iArray1[2] = 56;
iArray1[3] = 78;
Copy code

Example 2:


 
//declaration and initialization of the 1D-array
int[] numbers = {12, 4, 5, 2, 5};
Copy code

Syntax of Multidimensional array in Java:

syntax-2d
syntax1-2d

Example 1:


 
//declaration
int matrix[] = new int[2][2];
//initialization
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
matrix[1][1] = 4;
Copy code

Example 2:


 
//declaration and initialization of the 2D-array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
Copy code

Accessing Elements of Array in Java

  • Direct access to an element

You can directly access the exact element of an array using an index number.

Syntax:

syntax1

Example:


 
//directly accessing the second element of the array using index number
iArray[1];
Copy code
  • Accessing elements using for loop

An array can also be accessed through iteration statements such as for loop.

Example:


 
class MainDemo {
public static void main(String[] args) {
// create an array
int[] numbers = {1, 4, 5, 8, 7};
// using for loop go through the array
for(int i = 0; i < numbers.length; i++) {
System.out.println(age[i]);
}
}
}
Copy code
  • Accessing elements using for-each loop

For-each interaction loop can also be used to access elements of the array.

Example:


 
class MainDemo {
public static void main(String[] args) {
// create an array
int[] numbers = {1, 2, 4, 6, 8};
// using for-each loop to go through each element
for(int i : numbers) {
System.out.println(i);
}
}
}
Copy code
Methods to Convert Integer to String in Java
Java Lambda Expression
Understanding Java Scanner Class

Assigning an Array Reference to Another Array

As an array is a reference type variable, the references can be assigned to an already existing array as well. 

Example:


 
class MainDemo {
public static void main(String[] args) {
// create an array
int[] arr = {1, 2, 4, 6, 8};
int[] arr1;
arr1 = arr; //assigning arr array reference to arr1
System.out.println(arr1[3]);
}
}
Copy code

Output:

output1

Explanation:

Since the arr1 = arr, the content of arr is assigned to arr1. The arr is a reference variable that holds the address of the base memory location of the array. Therefore, the address copies to arr1 resulting in the output.

explanation

Also, Read: OOPs Concepts in Java

FAQs

What is the reference variable in Java?

It is used to point to a variable or object. The reference variable carries the address of the pointed variable or object.

What array in Java?

An array is an object in Java, and the array name (used for declaration) is a reference value that carries the base address of the continuous location of elements of an array. An array is used to hold elements of the same type.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio