Dec
21
2007

Java Tips: Array argument

This tips is not that advance. I expect that many have already known this and I even have already write a tips of one nice application of this technique. For those who don’t, Java 5 provides a nicer way to define an array argument.

Say there is a method like this:

public void processArray(int[] is) {
   // ...
}

With the old technique, you have to create the array explicitly before passing it as argument, such as:

processArray(new int[] {1,2,3,4,5});

With the new technique, you can simplify it to:

processArray(1,2,3,4,5);

But the method defined before have to be changed to:

public void processArray(int... is) {
   // ...
}

Of course, there are several requirements for this to happen.

  1. There is only one array that you want to change in the method
  2. The argument of array have to be placed in the very end of the method declaration
Written by Nanda Firdausi in: java, programming | Tags: ,

No Comments »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Powered by WordPress. Theme: TheBuckmaker. PHP Scripts, OpenID