[java] generics problem

Started by
15 comments, last by Michael Nischt 17 years, 8 months ago
I am trying to learn generics but it gives me an error when I try to compile this code. It says

<T>printScores(T[]) in ArrayTest cannot be applied to (int[])
printScores(testScores);
^


public class ArrayTest{

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

	public static void main(String[] args){
	
		int[] testScores;
		testScores = new int[4];
	
		printScores(testScores);
	
	}
	
}

I know that I have a local array that doesn't have anything in it yet so I don't know if that's the problem or not, but I am sure it will be down the road. If anyone can help me I would appreciate it very much, Thanks.
If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
maybe you have to explicitly instantiate it?

public static void main(String[] args){			int[] testScores;		testScores = new int[4];			printScores<int>(testScores);		}
Quote:Original post by Anonymous Poster
maybe you have to explicitly instantiate it?

*** Source Snippet Removed ***


public static void main(String[] args){			int[] testScores;		testScores = new int[4];			printScores<int>(testScores);		}


I forgot to loggin :(

VetroXL
Last time I checked, Java generics did not support primitive types, only things descended from Object. So no int.
It says

not a statement.printScores<int>(testScores);; expectedprintScores<int>(testScores);
If you insist on saying "DUH", try to make a post that makes a little more sense
Quote:Original post by SiCrane
Last time I checked, Java generics did not support primitive types, only things descended from Object. So no int.


Okay so there is the answer right there.
If you insist on saying "DUH", try to make a post that makes a little more sense
Quote:Original post by SiCrane
Last time I checked, Java generics did not support primitive types, only things descended from Object. So no int.


Actually, primitive types have been supported since the Generics system was first introduced. Auto-boxing was implemented at the same time, so anywhere an object is expected you can pass a primitive type and it will be auto-boxed to an instance of the class for that type (int will be promoted to Integer, float to Float, and so on).
Quote:Original post by cptrnet
I am trying to learn generics but it gives me an error when I try to compile this code. It says

*** Source Snippet Removed ***



*** Source Snippet Removed ***

I know that I have a local array that doesn't have anything in it yet so I don't know if that's the problem or not, but I am sure it will be down the road. If anyone can help me I would appreciate it very much, Thanks.


While primitive types can be auto-boxed to object instances when used singly, this apparently doesn't work for arrays of primitive types. What you'll have to do is make an array of Integer objects and use it instead:

public class ArrayTest{	public static <T> void printScores(T[] scores){		for(T t : scores){			System.out.println(t);		}	}	public static void main(String[] args){			Integer[] testScores = new Integer[4];                                // you can populate the array with regular ints- they                // will automatically be converted to Integer objects                for(int i=0; i<4; ++i)                    testScores = i;			printScores(testScores);		}	}
Interesting find. Do you think that was intentional?
Quote:Original post by Kevinator
Interesting find. Do you think that was intentional?


I'm sure it was, but I can only guess as to why. I'm sure it has to do with the fact that arrays are objects themselves. Conceptually, you can think of an array of ints as a type of IntArray, and array of Integers as a type of IntegerArray. All arrays expose the public methods of Object, implement the Cloneable interface, and have a public final field called length. I imagine that this adds some amount of complexity to auto-boxing from a primitive aray to an Object array, and likely would involve too much overhead to do behind the scenes.

This topic is closed to new replies.

Advertisement