开发者

How can you write a function that accepts multiple types?

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


With autoboxing / autounboxing, you can do this:

public static 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.


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);
    }
}


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)


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]);
}


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


class MyClass {
    public static void main(String[ ] args) {
        String name ="David";
        int age = 42;
        double score =15.9;
        char group = 'Z';
        System.out.println(group  + "\n" + name + " \n" + age + " \n" + score);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜