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]

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
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;
}
}