aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array" Watch "array" New topic
Author

array

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Here is the code:

class A{
public static void main(String[] args){
doStuff(1,2);
}
static void doStuff(int[] doArgs){}

}


My question is why can't we use doStuff(int[]) here?

Thanks..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The doStuff method you have declared takes an array of integers as its parameters. You are trying to call it with two seperate integers, not an array of integers.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
All right..!
So, how can we call doStuff through array of integers?
Can you please make required changes to the method call to make it work?
Thanks.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
I think, I got it..!
Modified the code below:

class A{
public static void main(String[] args){
int[] a = new int[]{1,2};
doStuff(a);
}
static void doStuff(int[] doArgs){}

}


Is this the only way you can pass an array as an argument to a method?
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
Alternatively, you could have moved away from the array and used variable length arguement list as following:

class A{
public static void main(String[] args){
doStuff(1,2);
}
static void doStuff(int... doArgs){}
}


If at first you don't succeed, try, try again. If you don't try you have failed.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Right..
Thanks..!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array