Jimmy Lillegaard
Jimmy Lillegaard

Reputation: 129

Method with 3 parameters but accepting one?

Is there a way to make Method that takes 3 or more parameters but can accept 1?

public void getAllSongs(String one, String two, String Three){
dosomething
}
getAllSongs("tada");

maybe not the best way to explain but i dont how else. I want use the same method with in more ways.. is it even possible?

Upvotes: 2

Views: 834

Answers (8)

cнŝdk
cнŝdk

Reputation: 32145

Yes of course you can, it's called Polymorphism Or more specificly it is Method Overloading:

In Java, it is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This concept is known as Method Overloading.

Take a look at the Polymorphism in Java – Method Overloading and Overriding.

Example:

void demo (int a) {
   System.out.println ("a: " + a);
}
void demo (int a, int b) {
   System.out.println ("a and b: " + a + "," + b);
}

Upvotes: 0

Bine
Bine

Reputation: 394

You can solve it by using (overloading) the method with a different number of input values. The user can call the one with just one/two/three... values. This one calles the one with more values if necessary. It could be something like this:

public void getAllSongs(String one){
  getAllSongs(one, null, null)
}

public void getAllSongs(String one, String two){
  getAllSongs(one, two, null)
}

public void getAllSongs(String one, String two, String Three){
dosomething
}

A call could be:

getAllSongs("bla","blub");

Another possibility by using arrays:

public void getAllSongs(ArrayList<String> songs){ 
do something 
}; 

List<String> songs= new ArrayList<String>();
songs.add("bla");
songs.add("blub");
getAllSongs(songs);

The ArrayList is an aarray with a dynamically size dependent on the number of values in it. Within your function you can iterate about this array

Upvotes: 0

theAnubhav
theAnubhav

Reputation: 533

NO. But its possible in C++/C# (assign parameters with a default values) Eg. :

public void getAllSongs(String one, String two=string.Empty, String Three=string.Empty){
dosomething
}
getAllSongs("tada");

But alternatively, You may use Pattern

    public void getAllSongs(String one, String two, String Three){
        dosomething
        }
    public void getAllSongs(String one, String two){
        getAllSongs(one,two,"");
        }
    public void getAllSongs(String one){
        getAllSongs(one,"","");
        }
        getAllSongs("tada");

Upvotes: 0

Marko Živanović
Marko Živanović

Reputation: 906

Overload the same method with different number of arguments, like this:

public void getAllSongs(String arg1) {
    getAllSongs(arg1, "default arg2", "default arg3");
}

public void getAllSongs(String arg1, String arg2) {
    getAllSongs(arg1, arg2, "default arg3");
}

public void getAllSongs(String arg1, String arg2, String arg3) {
    // do stuff with args
}

Upvotes: 0

Tinh Cao
Tinh Cao

Reputation: 67

i don't know exactly what you want to ask. But i think it should be

    public void getAllSongs(String... number) {
    // do something
    }

Upvotes: 0

Kamel Badni
Kamel Badni

Reputation: 64

it possible to call a method with a variable number of arguments like this

public void getAllSongs(String ... songs)

Upvotes: 2

Jens
Jens

Reputation: 69440

You can use Method overloading:

public void getAllSongs(String one ){
  getAllSongs(one,null,null);
}

public void getAllSongs(String one, String two, String Three){
dosomething
}
getAllSongs("tada");

Upvotes: 4

joey.enfield
joey.enfield

Reputation: 1229

A different approach would be to write you code like

public void getAllSongs(String... songs){
    for(String song : songs){
       //do somethihg
    }
}

This way you can call your code like

getAllSongs("song");
getAllSongs("song1", "song2"......)

Upvotes: 6

Related Questions