Tuesday, 10 July 2018


                                                     One Dimensional Array in java

public class OneDimensionalArray
{
  public static void main(String args[])
  {
    int marks[] = new int[5];

  System.out.println("Elements are " + marks.length);           

 // Now assign values
   
    marks[0] = 68;
    marks[1] = 70;
    marks[2] = 76;
    marks[3] = 85;
    marks[4] = 94;

  System.out.println("Value of 1st element: " + marks[0]);
  System.out.println("Value of 2nd element: " + marks[1]);
  System.out.println("Value of 3rd element: " + marks[2]);
  System.out.println("Value of 4th element: " + marks[3]);
  System.out.println("Value of 5th element: " + marks[4]);           

    System.out.println("Printing through  for  loop ");
    for(int i = 0; i < marks.length; i++)
    {
       System.out.println(marks[i] ); 
    }

    System.out.println("Printing through for each loop");
    for(int k : marks)
    {
        System.out.println(k);
    }   
  }
}





No comments:

Post a Comment