Monday, December 2, 2013

Teaching Arrays in Java

Array is a simplified data structure that contains a set of elements of same data type. Array elements are accessed through array's index.

However, caution must be considered since array normally produces pitfalls like accessing an array element outside the size of an array.

So, if I declare an array of 5 names, I can declare in Java as:

  String[] name = new String[5]; or String[] name = {"Maria","Jo","Sean","Abby","Ben"}; 

To display these elements, any looping command like for statement can be used.

The codes below can display these elements:
  for (int x = 0; x<5 b="" nbsp="" x="">
    System.out.print(" "+name[x]);

 In Java, the first element is indexed at 0 and the last element at size - 1.

No comments: