Basic Java – 8 || Lets learn about Arrays in Java.

        In general, “array” means an arrangement of similar things in a particular way. On the other hand, we can say the arrays in Java is grouping the things having same features.

               In Java programming, the array is used to store multiple values in a single variable. We can also say an array stores multiple variables of same data types in a single unit.

Array_Single Dimension
Array_Single Dimension
Related Links:

OOPs Concept:

Java Question And Answer:

Declare and initialize array

<DataType>[] <VariableName> =new <DataType>[ArrayLength]

<variableName>[index]=<variableValue>

OR

<DataType>[] <VariableName>={<value1, value2, value3….>}

Example:

int[] rollNumber=new int[5];

int[1]=50;

OR

int[ ] rollNumber={3,57,6,89,60}

Access values from an array:

int x=rollNumber[2];

‘x will store value 6 from the array.’

Process and Structure of Array

    1. DataType: Define data type of array variable.
    2. Square brackets[ ], denotes that currently declared variable is of array type.
    3. VariableName is the name of an array.
    4. new, the keyword used to initiate object of the expected data type.
    5. ArrayLength defines the total number of values, which can be stored in this array variable.
    6. [index], used to access and store values on a specific position in the array.

Limitation of Array

  1. Array allocated memory as defined in ‘ArrayLength’ field. So, it can store only given number of values. Length of array remains fixed.
  2. We can’t create a dynamic array, so the length of the array cannot be increased or decreased at run time.
  3. The memory remains occupied even after removing values from an array.
  4. We can access values in a specific position using index number. The index will always start with 0.

Types of Array

  1. Single Dimensional Array: Already described above.
  2. Two Dimensional Array: Process to initialize and access the two-dimensional array would be same as the single dimensional array.
Array_Two Dimension
Array_Two Dimension

              A two-dimensional array stores data in a table using row and column format. As in the above image, it is clear that we have declared a two-dimensional array having 3 rows and 2 columns.

                                int[ ][ ] arrayVar = new int[row][col];

           In a two-dimensional array, the first index depicts row and second index shows column count. Note that both the index would always start from 0.

Example:

Int[ ][ ] rollNumber=new int[5][2];

int[1][0]=50;

OR

int[ ][ ] rollNumber = {

                                 {10,9},

                                 {8,7},

                                 {6,5},

                                 {4,3},

                                 {2,1} };

Access values from an array:

int x=rollNumber[2][1];

‘x will store value 5 from the array.’

Code Example Single Dimensional Array

public class SingleDim {
	public static void main(String[] args)
	{
		// Declare array variable with length 5.
		int[] rollNumber=new int[5];
		
		// Initialize and assign values to array variable for all positions using index.
		rollNumber[0]=10;
		rollNumber[1]=9;
		rollNumber[2]=8;
		rollNumber[3]=7;
		rollNumber[4]=6;
		
		// Extracting and storing values in other variables
		int temp1=rollNumber[2];
		int temp2=rollNumber[4];
		
		// Print values from other variables
		System.out.println("temp1: "+temp1);
		System.out.println("temp2: "+temp2);
		
		
		// We can also access the array values directly
		System.out.println("\nDirect access of array values:");
		System.out.println(rollNumber[0]);
		System.out.println(rollNumber[1]);
		
		System.out.println("\nAlternate proecss to declare and initialize a array variable :");
		int[] tempArray={5,4,3};
		
		System.out.println(tempArray[0]);
		System.out.println(tempArray[1]);
		System.out.println(tempArray[2]);
		
		// If we try to access an undefined index value, it will throw 'ArrayIndexOutOfBoundExceptin'
		System.out.println(tempArray[10]);
	}
}

Code Example of Two-Dimensional Array

public class TwoDim {
	public static void main(String[] args)
	{
		        // Declare Two dimensional array variable with 3 row and 2 Columns
				int[][] rollNumber=new int[3][2];
				
				// Initialize and assign values to array variable for all positions using index.
				rollNumber[0][0]=10;
				rollNumber[0][1]=9;
				rollNumber[1][0]=8;
				rollNumber[1][1]=7;
				rollNumber[2][0]=6;
				rollNumber[2][1]=5;
				
				
				// Extracting and storing values in other variables
				int temp1=rollNumber[1][1];
				int temp2=rollNumber[2][0];
				
				// Print values from other variables
				System.out.println("temp1: "+temp1);
				System.out.println("temp2: "+temp2);
				
				
				// We can also access the array values directly
				System.out.println("\nDirect access of array values:");
				System.out.println(rollNumber[0][0]);
				System.out.println(rollNumber[1][0]);
				
				System.out.println("\nAlternate proecss to declare and initialize a array variable having 2 rows and 2 columns :");
				int[][] tempArray={
						{5,4},
						{3,2}
						};
				
				System.out.println(tempArray[0][0]);
				System.out.println(tempArray[0][1]);
				System.out.println(tempArray[1][0]);
				System.out.println(tempArray[1][1]);
	}
}

1285total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *