In: Computer Science
Describe how Java creates a single dimension array and a double dimension array.
Describe and explain how Java processes the contents of a single dimension array.
Describe and explain how Java saves values to a single dimension array, and to a double dimension array.
Array: Array is a collection of similar data types. In array all elements are the same data type. In array all elements are used the same data variable name but each element has own unique index number like a[0]. Array can use in all programming languages.
Type of array: two type of array
Single Dimensional Array: one-dimensional array is a list of similar data types that are accessed by a single variable name. one-dimensional array is like a list. All elements are in a line.
Example of one-dimensional array and how it will declaration initialize and represent.
Declaration of the 1D array: Declaration and initialize of allocating memory to an array
Syntex: Datatype array_name = new datatype[size]
value syntex : array_ name [ ]= value ;
// initialize the first elements of the array
arr[0] = 1 // initialize the second elements of the array
arr[1] = 2 //so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
example: int arr[] = new int[3]
{ where int = data type and arr[] = array name , 3 is number of space allocation in memory}
Use in program :
Multidimensional Array: If we want to save similar type of data in row and colume like table then we use 2D array. The most basic multidimensional array is 2D array.
let's see the example how we declare , initialize, and print 2d array.
Declaration of the 2D array: Declaration and initialize of allocating memory to an array
Syntex: Datatype array_name[ ][ ]= new datatype[ ][ ] ;
Initaialize: array_name[row_index][column_index] = value;
For example: arr_name[0][0] = 1;
Example: int arr_name[ ][ ] = new int[3][3]; // we allocate 3 row and 3 column in memory
Use of Two-Dimension Array in Program:
class class_name { // class intianlize
public static void main(String[] args)
{
int[][] arr = { { 1, 2 }, { 3, 4 } }; // value initianlize
for (int i = 0; i < 2; i++) // for loop for row
{
for (int j = 0; j < 2; j++) // for loop for column
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}