Various ways of accessing an array
Today i did learn two ways of accessing the array, i would like to know the various ways of accessing an array element and the best practice of it. I am a student learning algorithm.
int [] arr;
long [] arr;
Advantages of long datatype declaration over int.
class ArrayApp{
publ开发者_运维技巧ic static void main(final String[] args){
long [] arr;
arr= new long[2];
int i;
arr[0]=112;
arr[1]=111;
**// Way one**
for(long l:arr)
{
System.out.println(l);
}
**// Way Two**
for(i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
There is no real difference between the ways here. Way one is just a syntax sugar for not to create an additional interation value.
The first way is preferable as it doesn't require an int i;
variable and requires less printing. The second should be used when you don't want to iterate through the all array, but just a part of it.
There is no other ways to access the elements of array in java.
We've one declaration of an array:
long[] values = new long[100];
This creates an array for 100 long
type values. Each value has an index (position) inside the array, the first index (an int
value!) is 0, the last one is 99.
The traditional for loop increments an integer value to generate index position numbers. Those int
values are used to access the long
values of the array:
for (int index=0; index < values.length; index++) { // index is int
long value = values[index]; // value is long
// do something with value
}
The "enhanced" for loop simply hides this index and gives access to the long
values with less code:
for (long value:values) {
// do something with value
}
That's all. If you don't need the index variable in your code, just use the enhanced for loop (second version in my answer)
The first way
for(long l:arr)
{
System.out.println(l);
}
The java runtime will autobox the l to a Long, as iterating this way requires that the class implement the Iterable interface. This way, also does not provide the current index of the array.
The second way
for(i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
Requires no casts to Long and you also have access to the current index. You also however have to be careful about iterating past the end of the array or accessing array elements below 0.
An int is shorter as a long. You can make an array of any object or primitive type. int[], Integer[], CustomClass[], whatever.
From java documentation:
int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
精彩评论