Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

var-args and arrays........

 
Jaspreet Oberoi
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Arrrrg{
public static void main(String... args){
int [] a = {1,2,3};
doIt(1);
doIt(1,2); //1
doIt(a); //2
}

static void doIt(int... params){System.out.println(" 1 ...]");}
static void doIt(int [] params){ System.out.println(" 2 []");}
}

Can anyone please guide me how to execute doIt() on an int[] argument , if I already have var agrs argument method .
The compilor refused to compile with an error .
I mean I want both 1 and 2 to work ... Please point the error.
 
Jay Ashar
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doIt(int... params) method takes in array of ints and its size would depend on number of ints you pass.
doIt(int[] params) also takes an array of int and you cannot define 2 methods with same signature in same class, therefore you get an error saying doIt(int... p) is already defined in class.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Recently we discussed the same issue with a lot of code examples and explanations.

I think this thread may be useful for you guys.
 
Oscar Gonzalez
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong Post


[ March 30, 2006: Message edited by: Oscar Gonzalez ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oscar,

I don't think so.

Once the thread I've posted contains some explanations and code about the problem stated by Jaspreet. I think this link would be useful to help them in some way.

By the way, I really could understand about Java treatment for int [] and int ... after discuss with all other colleagues through the thread I've posted before.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic