Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

[java] generics problem

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
maybe you have to explicitly instantiate it?


public static void main(String[] args){

int[] testScores;
testScores = new int[4];

printScores<int>(testScores);

}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Last time I checked, Java generics did not support primitive types, only things descended from Object. So no int.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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);

}

}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!