views:

133

answers:

5

I have a function that should work on int[] and on String[] now i have made the same function with a int parameter and an String parameter however if it has to go this way its a bit copy paste work and doesn't look very organized is there a way to solve this and put these 4 functions in 2?

    static public void print(String s)
    {
        System.out.println(s);
    }

    static public void print(int s)
    {
        System.out.println(s);
    }

    static public void printArray(String[] s)
    {
        for (int i=0; i<s.length; i++)
            print(s[i]);
    }

    static public void printArray(int[] s)
    {
        for (int i=0; i<s.length; i++)
            print(s[i]);
    }

Thanks Matthy

+7  A: 
static public void print(String...strings){
    for(String str : strings){
        System.out.println(str);
    }
}

static public void print(int...ints){
    for(int i : ints){
        System.out.println(i);
    }
}
Rick
i meanth the oposite like what danben said but this looks interesting too is it possible to combine these 2 method's so you have only one function?
matthy
+1 for being brain dead easy to read
Dolph
A: 

Well, the Basic class java.lang.Object matches String as well as int, byte, ... (Autoboxing converts them to Integer, Byte and so on). The method String.valueOf() lets you create a string of these. (toString() is available too)

ZeissS
+5  A: 

With autoboxing / autounboxing, you can do this:

static public void print(Object s) {
  System.out.println(s.toString());
}

public static <T> void printArray(T[] arr) {
  for (T t : arr) {
    print(t);
  }
}

The one drawback is that the argument to printArray must be an array of a reference type, but unlike the varargs solution this will work for any reference type.

Edit: regarding the varargs solution and @matthy's question about combining the two methods into one (ie generifying it), you could also do something like this:

static public <T> void print(T... ts) {
    for (T t : ts) {
      System.out.print(t + " ");
    }
    System.out.println("");
}

However, you still cannot call it on an array of primitives:

    int[] x = { 1, 2 };
    print(x);

Because Java takes T to be int[] and will execute the toString method of the array rather than iterate through the contents. If you call it on an array of Integer or other reference type then it will work also.

danben
A: 

Use Generics, if applicable to your code:

static <T> void printArray(T[] s)
{
    for (int i=0; i<s.length; i++)
        System.out.println(s[i]);
}
PartlyCloudy
A: 

Combining the previous answers yield:

public class Test {

    static <T> void print(T... ts) {
        for (T t : ts)
            System.out.println(t);
    }

    public static void main(String... args) {
        String[] strArr = { "one", "two" };
        Integer[] intArr = { 3, 4 };
        String str = "five";
        int i = 6;

        print(strArr);
        print(intArr);
        print(str);
        print(i);
    }
}

Output:

one
two
3
4
five
6
aioobe