New to java - issue with generics and arrays - any ideas?

Hoping someone can give me a hand with this...I'm getting the error "<T>countOccurrences(T[],java.lang.Object) in Driver cannot be applied to (int[],int)"

publicclass Driver

{

privatestaticint[] x ={1,2,3,3,8,4,0,9,9,4,6};

privatestaticint data = 9;

publicstaticvoid main()

{

System.out.println(countOccurrences(x,data));

}

static <T>int countOccurrences(T[] array, Object data)

{

int count = 0;

if (arr !=null){

for (T item : array){

if (array[item].equals(data)){

count++;

}

}

}

return count;

}

}

[1656 byte] By [Devina] at [2007-11-26 18:14:06]
# 1
In Generics the T symbol is used to represent any object. Ints are primitives not objects.
floundera at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 2
Is there a way to work in a generic type of somekind that goes for objects AND primitives?thank you for the quick reply...I cant find information pertaining to this in my book and google isnt sifting through the right stuff at the moment for me.
Devina at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 3
You do know that arrays are objects, right?
CaptainMorgan08a at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 4

apologize for my ignorance. Yes I do know that. I believe it has something to do with data being a primative though and possibly needing to specify Integer somewhere...though im probably completely wrong.

This is my updated code...

public class Driver

{

private static int[] x = {1,2,3,3,8,4,0,9,9,4,6};

private static int data = 9;

public static Integer main()

{

System.out.println(countOccurrences(x,data));

}

static <T> int countOccurrences(T[] array, T data)

{

int count = 0;

if (arr != null) {

for (T item : array) {

if (array[item].equals(data)) {

count++;

}

}

}

return count;

}

}

Message was edited by:

Devin

Devina at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 5
public static Integer main()Never mess with the main() method. It always is void and takes an array of Strings.
CaptainMorgan08a at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 6

Sorry, made your suggested change but that doesnt change anything as far as the error. I am now at: <T>countOccurrences(T[],T) in Driver cannot be applied to (int[],int)

Are there problems passing primitives through generic methods? If so, is there a way to get around it?

public class Driver

{

private static int[] x = {0,4,8,6,1,3,5,8,3,4,5,6,8,9,9,5,6,7,4,3,5,7,7,3};

private static int data = 9;

public static void main(String[] args)

{

System.out.println(countOccurrences(x,data));

}

static <T> int countOccurrences(T[] array, T data)

{

int count = 0;

if (arr != null) {

for (T item : array) {

if (array[item].equals(data)) {

count++;

}

}

}

return count;

}

}

Devina at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 7
Think I got past it by redeclaring my ints as IntegersThank you for the quick help guys...got me on the right track.
Devina at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 8
> Are there problems passing primitives through generic methods?Yes! See reply #1.> If so, is there a way to get around it?Yes! Make your primitives objects by using the wrapper class.
floundera at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...
# 9
Got it, thank you very much. Looking at the wrapper class now.DM
Devina at 2007-7-9 5:47:24 > top of Java-index,Java Essentials,New To Java...