Accessing of one dimensional Array

As you already know one-dimensional array in java uses the singe subscript([]). In this post, we will see how accessing array elements works. You can access an array’s element with the name of the array variable, followed by brackets([]). You must specify an integer value in brackets([]) called the index of an array.

arrayName[subscript]; 
class AccessOfArray
{
   public static void main(String[] args) 
   {
       int[] number= {11, 22, 33, 44, 55, 66, 77, 88, 99, 100}; 
       int length  = number.length; // get size of array
       for (int i = 0; i < length; i++) 
       {
         System.out.println("Index of element = " + i +" & Element is = " +number[i]);
       }
   }
}

Output:
Index of element = 0 & Element is = 11
Index of element = 1 & Element is = 22
Index of element = 2 & Element is = 33
Index of element = 3 & Element is = 44
Index of element = 4 & Element is = 55
Index of element = 5 & Element is = 66
Index of element = 6 & Element is = 77
Index of element = 7 & Element is = 88
Index of element = 8 & Element is = 99
Index of element = 9 & Element is = 100

accessing array elements

Accessing array elements in java

  1. To access any element from the array you must use an array name with subscript and subscript takes an index. Remember one thing here, an index should be always in an integer value.
  2. In array index value is always start from 0 and end with array’s length-1. So, when you are accessing an array you must take of the length of an array. If you are trying to access an element, then index value should be less than the length of an array.
  3. All the elements can access via for loops in Java.

Changing elements of Array: You can assign new value and replace the old value by using subscript with an array name. To assign a value at index, you can assign value by using an assignment statement:

arrayName[subscript] = value;
class ArrayClassExample 
{
   public static void main(String[] args) 
   { 
        // Initiation an array at declaration time
      	String[] names = { "Ravi", "Ram"};
     	int length  =  names.length;
      	// Print all names before changes
      	for (int i = 0; i < length; i++) 
	   System.out.println("Names of Students = " + names[i]);
	
    	// Changing value of elements
	names[0] = "Radhe";
	// Print all names before changes
	for (int i = 0; i < length; i++) 
	    System.out.println("After changes names of Students = "+ names[i]);
     }
}

Output:
Names of Students = Ravi
Names of Students = Ram
After changes names of Students = Radhe
After changes names of Students = Ram

1. Quiz, Read the below code and do answer.

public class AccessOfArray
{
   public static void main(String[] args) 
   {
       int[] number= {11, 22, 33, 44, 55, 66, 77, 88, 99, 100}; 
         System.out.println("Element is = " +number[0]);
   }
}

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class AccessOfArray
{
   public static void main(String[] args) 
   {
       int[] number= {11, 22, 33, 44, 55, 66, 77, 88, 99, 100}; 
       for(int i = 0; i < number.length-1; i++)
    	   System.out.println("Element is = " +number[i]);
   }
}

Click on anyone to know the answer.

Leave a Comment