Array-In-Java 11
Array-In-Java 11
Topperworld.in
Array in Java
Arrays in Java are declared with a specific data type, followed by square
brackets “[ ]” indicating the size of the array.
Advantage of Array
Easy to use: Arrays are easy to use and implement, making them a
popular choice among programmers.
Fast access: Arrays provide fast and efficient access to elements
based on their index, which makes them ideal for storing and
retrieving data quickly.
Memory efficiency: Arrays are memory-efficient, as they store data
in a contiguous block of memory, which makes them ideal for handling
large amounts of data.
Easy to manipulate: Arrays can be easily manipulated using loops,
making it easy to perform operations on all elements of an array.
Java Programming
Disadvantage of Array
Fixed size: Arrays in Java are of fixed size, which means that the size
of the array cannot be changed once it is initialized.
Lack of flexibility: Arrays cannot be resized dynamically, which
means that if you need to add or remove elements from an array, you
need to create a new array with a different size.
Inefficient for certain operations: Arrays are inefficient for certain
operations, such as sorting and searching, as these operations can
require a lot of computational power and time to execute.
Complex data types: Arrays are not suitable for storing complex data
types, such as objects and structures, as they can only store a single
data type.
Types of Array
Single-Dimensional array
Multi-Dimensional array
Single-Dimensional Array :
dataType[] arrayName;
dataType - it can be primitive data types like int, char, double, byte,
etc. or Java objects
arrayName - it is an identifier
For example,
double[] data;
// declare an array
double[] data;
// allocate memory
data = new double[10];
Here, the array can store 10 elements. We can also say that the size or
length of the array is 10.
In Java, we can declare and allocate the memory of an array in one single
statement.
For example,
Here, we have created an array named age and initialized it with the values
inside the curly brackets.
Note that we have not provided the size of the array. In this case, the Java
compiler automatically specifies the size by counting the number of elements
in the array (i.e. 5).
In the Java array, each memory location is associated with a number. The
number is known as an array index. We can also initialize arrays in Java,
using the index number. For example,
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..
Note:
Array indices always start from 0. That is, the first element of an array
is at index 0.
If the size of an array is n, then the last element of the array will be at
index n-1.
We can access the element of an array using the index number. Here is the
syntax for accessing elements of an array,
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
Output
Multi-Dimensional Array :
2-dimensional Array
Remember, Java uses zero-based indexing, that is, indexing of arrays in Java
starts with 0 and not 1.
Let's take another example of the multidimensional array. This time we will
be creating a 3-dimensional array. For example,
type String.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
Java Programming
// create a 2d array
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
Output:
Length of row 1: 3
Length of row 2: 4
Length of row 3: 1
Java Programming
class MultidimensionalArray {
public static void main(String[] args) {
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
System.out.println(a[i][j]);
}
}
}
}
Output:
1
-2
3
-4
-5
6
9
7
class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d array
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
Output:
-2
-4
-5
Let's see how we can use a 3d array in Java. We can initialize a 3d array
similar to the 2d array. For example,
// test is a 3d array
int[][][] test = {
{
{1, -2, 3},
{2, 3, 4}
},
{
{-4, -5, 6, 9},
{1},
{2, 3}
}
};
class ThreeArray {
public static void main(String[] args) {
// create a 3d array
int[][][] test = {
{
{1, -2, 3},
{2, 3, 4}
},
{
{-4, -5, 6, 9},
{1},
{2, 3}
}
};
Java Programming
Output:
1
-2
3
2
3
4
-4
-5
6
9
1
2
3