How can you write a function that accepts multiple types?

Posted by matthy on Stack Overflow See other posts from Stack Overflow or by matthy
Published on 2010-05-26T16:05:17Z Indexed on 2010/05/26 16:11 UTC
Read the original article Hit count: 320

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about variables