File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
Lovleen Gupta
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){}
}
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right..
Thanks..!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic