|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
|
A common programming task is iterating over the elements of a collection or an array. An example is shown in the code fragment below:
// numbers is a list of Numbers
for (Iterator it = numbers.iterator(); it.hasNext(); )
{
Integer number = (Integer) it.next();
// Do something with the number...
}
The above code fragment, although effective, is cumbersome. Version 1.5 enhances the for statement to accept a more evolved syntax (the previous code still works) such as:
for(Integer number: numbers)
{
// Do something with the number...
}
The above code actually compiles into code similar to the first code fragment. The enhanced for loop also works with arrays. Personally, I would have preferred a new keyword such as "foreach." This is actually a FAQ,
and Sun has responded to it by saying that creating a new keyword would be too "costly." Sun has, however, added keywords
before, such as the assert keyword in 1.4 and the new enum keyword (we'll see this one shortly) in 1.5. So, I am not too convinced by this answer. Finally, remember that if you wish
to modify the collection (such as remove elements) while traversing it, you cannot use the enhanced for loop.
Often, having a method that can operate on a variable number of parameters is convenient. A commonly accepted way of accomplishing that is to define the method so it accepts an array or collection of objects. An example code fragment is shown below:
// An example method that takes a variable number of parameters
int sum(Integer[] numbers)
{
int mysum = 0;
for(int i: numbers)
mysum += i;
return mysum;
}
// Code fragment that calls the sum method
sum(new Integer[] {12,13,20});
While this proves effective in simulating a method that can take a variable number of parameters, it seems clunky and involves slightly more (unnecessary) work by the programmer. Fortunately, that is no longer required in version 1.5 thanks to the new variable arguments (varargs) feature. Let's rewrite the above code fragments using varargs.
// An example method that takes a variable number of parameters
int sum(Integer... numbers)
{
int mysum = 0;
for(int i: numbers)
mysum += i;
return mysum;
}
// Code fragment that calls the sum method
sum(12,13,20);
Note the change in the method signature. Instead of explicitly taking an array of Integer objects, we inform the compiler that a variable number of Integer objects will be passed using an ellipsis (...). Note that the actual code inside the method does not change. Finally, notice how much cleaner (and simpler) the method
call becomes. A perfect example of where this feature has been used by version 1.5 itself is in the implementation of the
C-style formatted output method printf:
// Using System.out.println and System.out.printf
int x = 5;
int y = 6;
int sum = x + y;
// Pre 1.5 print statement
System.out.println(x + " + " + y + " = " + sum);
// Using 1.5 printf
System.out.printf("%d + %d = %d\n", x, y, sum);
Both statements print the same line to the console—5 + 6 = 11. You can do a lot more with the extremely powerful and versatile printf method. Look at the following code fragment:
Archived Discussions (Read only)
(