Implementing Array in Java
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.
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
- What is an Array?
- Pros and Cons of Using Array
- Types of Array in Java
- How to Declare and Initialize an Array in Java?
- Accessing Elements of Array in Java
- Assigning an Array Reference to Another Array
Letβs begin with an understanding of arrays in Java.
Best-suited Java courses for you
Learn Java with these high-rated online courses
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.
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];
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:
- Single Dimensional Array β One-dimensional arrays are organized in a linear list in memory. Their indexes run from 0 total array size-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.
How to Declare and Initialize an Array in Java?
Syntax of One-dimensional array in Java:
Example 1:
//declarationint iArray1[] = new int[4]; //initializationiArray1[0] = 32;iArray1[1] = 13;iArray1[2] = 56;iArray1[3] = 78;
Example 2:
//declaration and initialization of the 1D-arrayint[] numbers = {12, 4, 5, 2, 5};
Syntax of Multidimensional array in Java:
Example 1:
//declarationint matrix[] = new int[2][2]; //initializationmatrix[0][0] = 1;matrix[0][1] = 2;matrix[1][0] = 3;matrix[1][1] = 4;
Example 2:
//declaration and initialization of the 2D-arrayint arr[][]={{1,2,3},{2,4,5},{4,4,5}};
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:
Example:
//directly accessing the second element of the array using index numberiArray[1];
- 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]); } }}
- 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); } }}
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]); }}
Output:
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.
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.
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