This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes generics problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generics problem" Watch "generics problem" New topic

generics problem

Andy Bliss

Joined: Mar 27, 2006
Posts: 1
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[])

public class ArrayTest{

public static <T> void printScores(T[] scores){
for(T t : scores){

public static void main(String[] args){

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




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.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
The problem is an int[] is not a T[].
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Petrus Pelser:


That's not going to work.

The entity between < and > must be the type variable(s) in the generic method.
I agree. Here's the link:
subject: generics problem
It's not a secret anymore!