Wednesday, November 17, 2010

Accessing array elements in Java

  int[] primes=new int[10];       // Creating a new array object 

You can refer to an array by using the array name followed by the element's  index value closed in between sqyare brackets.You can specify an index value by ant expression that produce  zero or possitive result of type int . If you use a value type of long as an index, you will get an error message from the compiler, if  you calculate of an index uses long variables and the result is of type long, you will need to cast it to type int.

The maximum index value for an array is one less than the number of element in the array. JAVA checks that the index values you see are valid.if you use an index value that is less than 0, or greater than the index value for the element in the array an exception will be thrown. (Index out of bound exception)

The prime array which was taken as an example above is sometimes referred to as an one dimensional error because each of its elements is referenced using one index-running from 0 to 9 in this case......

No comments:

Post a Comment