Arrays in Java

In this tutorial we will learn about Arrays in Java. Till now we leaned about simple data type like int, float, char and variable. We know in simple variable we can store a value for example int num=10; Here num is variable of int type in which we can store integer value. but suppose you want to store 100 values. It mean you should have 100 variable. It will so though to create and manage 100 variables. so to overcome this kind of situations Java have a concept called Arrays.
In an Array we can store multiple values using single variable name. In an Array values get store according to index. Let's check syntax to define and initialize an array in Java.

  
    int marks[5]={20,25,24,30,15};
  

Arrays in Java


Index system of Array:


Arrays in Java
Array we discussed earlier how in array values store on index. In this figure you can see how values get stored on different index numbers. Index number always start from 0.

Access Array Element: To Array element we have to mention index number. Then we can fetch value of that particular in index in array. Check this Example.

Example
 
  
class ArrayExample{
  public static void main(String[] args) {
    int[] marks[5]= {10,15,12,19,20};
    System.out.println(marks[2]);
    //output would be 12 because 12 on index 2 (index starts from 0)
  }
}
  


Array with loop: Here we will check how we can use loop to access all the elements of array. Check this example.

Example
 
  
class ArrayExample{
  public static void main(String[] args) {
    int[] marks[5]= {10,15,12,19,20};
       for(int i=0;i<5;i++){
          System.out.println(marks[i]);
       }
   }
}
  




Share this tutorial with your friends on your social Media..

Arrays in Java Arrays in Java Arrays in Java Arrays in Java



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com