This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes var-args and arrays........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "var-args and arrays........" Watch "var-args and arrays........" New topic

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

Jaspreet Oberoi

Joined: Mar 30, 2006
Posts: 1
class Arrrrg{
public static void main(String... args){
int [] a = {1,2,3};
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

Joined: Oct 13, 2002
Posts: 208
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.

SCJP 1.4<br />SCWCD 1.3
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Oscar Gonzalez
Ranch Hand

Joined: Mar 28, 2006
Posts: 63
Wrong Post

[ March 30, 2006: Message edited by: Oscar Gonzalez ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.
I agree. Here's the link:
subject: var-args and arrays........
It's not a secret anymore!