Array, in The Context of Java, Is A Dynamically-Created Object That

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Jhimar P.

Calderon BSCS 2A

Java Arrays

An array is a container object that holds a fixed number of values of


a single type. The length of an array is established when the array is
created. After creation, its length is fixed. You have seen an
example of arrays already, in the main method of the "Hello World!"
application. This section discusses arrays in greater detail. An
array, in the context of Java, is a dynamically-created object that
serves as a container to hold constant number of values of the
same type. By declaring an array, memory space is allocated for
values of a particular type. ... An array element that is also an array
is known as a subarray.
Advantages

 We can access any element randomly by using indexes


provided by arrays.
 Primitive type to wrapper classes object conversion will
not happen so it is fast.
 Array can store many number of elements at a time.

Disadvantages

 Arrays are Strongly Typed.


 Arrays does not have add or remove methods.
 We need to mention the size of the array. Fixed length.
 So there is a chance of memory wastage.
 To delete an element in an array we need to traverse
throughout the array so this will reduce performance.
There are two types of arrays in Java:

1. One-dimensional array in Java programming is an array with a


bunch of values having been declared with a single index. As you can
see in the example given above, firstly, you need to declare the
elements that you want to be in the specified array. One-dimensional
array in Java programming is an array with a bunch of values having
been declared with a single index.

Declaration of Syntax

datatype array_name[] = {list of values};

2. Java Multidimensional Arrays. In Java, you can declare an array of


arrays known as multidimensional array. ... Here, a is a two-
dimensional (2d) array. The array can hold maximum of 12 elements
of type int . Remember, Java uses zero-based indexing, that is,
indexing of arrays in Java starts with 0 and not 1.

Two – dimensional Array (2D-Array)


Two – dimensional array is the simplest form of a
multidimensional array. A two – dimensional array can be seen
as an array of one – dimensional array for easier understanding.

Declaration Syntax:
data_type[][] array_name = new data_type[x][y];
For example: int[][] arr = new int[10][20];
Three Dimensional Array Program

Three dimensional array is a complicated concept in programming that


contains three loops. So to print or initialize a three dimensional array,
you have to use the three loops. The innermost loop makes one
dimensional array and the second innermost loop contain the two
dimensional array whereas the outer loop contains the three
dimensional array.

Declaration – Syntax:

data_type[][][] array_name = new data_type[x][y][z];

For example: int[][][] arr = new int[10][20][30];

One dimensional example:

// Average an array of values.


fclass Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}
Two dimensional example:
class TwoDimensionalArray {

public static void main(String[] args) {


String[][] salutation = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Kumar"}
};

// Mr. Kumar
System.out.println(salutation[0][0] + salutation[1][0]);

// Mrs. Kumar
System.out.println(salutation[0][1] + salutation[1][0]);
}
}

Three dimensional example

class GFG {

    public static void main(String[] args)

    {

int[][][] arr = new int[10][20][30];

      arr[0][0][0] = 1;

System.out.println("arr[0][0][0] = " + arr[0][0][0]);

    }

You might also like