dev
dev

Reputation: 2584

Generic method to print arrays in java

I am trying to write a generic method printAll which prints an array of integer or character. Here's the code:

public static void main(String[] args) {
    char cArray[] = {'a','b','c','d'};
    int iArray[] = {1,2,3,4};
    printAll(iArray);    // Error at this line--refer below the code

}

public static <T> void printAll(T[] t){
   for(T x:t) {
        System.out.println(x);
    }

}

Error: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <.any>

Upvotes: 0

Views: 4321

Answers (5)

Aiq Abushov
Aiq Abushov

Reputation: 21

class Generic {
    public static<T> void printArray(T[] list) {
        for(T in : list) {
            System.out.println(in);
        }
    }

    public static void main(String[] args) {
        String[] lit=new String[3];
        lit[0]="aiq";
        lit[1]="abusov";
        lit[2]="java";
        printArray(lit);
    }   
}

Upvotes: 0

Grbe1l
Grbe1l

Reputation: 489

I don't believe in doing peoples work for them, more to inform and teach how it can be done.

one easy and simple way to print arrays in java will be done through converting the array to a string then print. This works best as array lists. This can then be manipulated to print in a better format.

for example make an array list and add some values:

ArrayList<String> Array = new ArrayList<String>();
Array.add(1)
Array.add(2)
Array.add(3)
Array.add(4)
Array.add(5)

Then from here this can be printed by changing it to a string:

String str = Array.toString()
System.out.println(str);

this will print the following:

[1,2,3,4,5]

you can then change the format of this by using replaceALL on the string before you print it:

String str = Array.toString().replaceAll("[\\[\\]]","")

This will now give:

1,2,3,4,5

That is pretty much it, from here you could add extra things like System.lineSeparator which you would use to replace thhe "," with a new line.

String str = Array.toString().replaceAll("[\\[\\]]","").replaceAll(",",System.lineSeparator());

This would make it print out like:

1
2
3
4
5

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213223

printAll(T[] t) will not accept primitive type arrays. You need to pass arrays of the respective wrapper types:

Character cArray[] = {'a','b','c','d'};
Integer iArray[] = {1,2,3,4};

But, you don't need to frame your own method. Just use the already existing - Arrays.toString() method, which is overloaded for different types of primitive arrays, and Object[] array.

Upvotes: 8

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Do not reinvent the wheel, use Arrays.toString or Arrays.deepToString. The former already is overloaded to support arrays of primitive (as noted in the first link that receives a char[]), the latter works only on arrays of class references objects.

Upvotes: 7

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279930

You cannot use primitive types with generics. Use Integer (or the corresponding reference type)

Integer iArray[] = {1,2,3,4};
printAll(iArray);   

You can always overload the printAll method for each of the primitive types.

Upvotes: 1

Related Questions