[英]issue instantiating object of Java generic class
我一直在兜圈子,試圖了解Java泛型。 我在實例化泛型類的對象時遇到了麻煩。 對我要去哪里錯有任何見解嗎?
在一個文檔中,泛型類:
public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
…
public void quickSort(T[] list, int length)
{
recQuickSort(list, 0, length - 1);
}
…
}
在另一個:
public class TestQuickSort
{
public static void main(String [] args)
{
// define an Integer array of 50000 elements
Integer[] anArray = new Integer[5000];
// load the array with random numbers using
// a for loop and Math.random() method - (int)(Math.random()*50000)
for (int i = 0; i < anArray.length; i++)
{
anArray[i] = (int)(Math.random() * i);
}
// print the first 50 array elemnts with a for loop
// using System.out.print
for (int j = 0; j <= 50; j++) {
System.out.print(anArray[j] + " ");
}
System.out.println();
// define an object of SearchSortAlgorithm with Integer data type
// use this object to call the quickSort method with parameters: your array name and size-5000
SearchSortAlgorithms<Integer> anotherArray = new SearchSortAlgorithms<Integer>(); //This is where I get my error message
anotherArray.quickSort(anArray, 5000);
// print out the first 50 array elements with a for loop
// they have to be sorted now
for (int k = 0; k <= 50; k++) {
System.out.print(anotherArray[k] + " ");
}
}
}
錯誤信息:
java:39: array required, but SearchSortAlgorithms<java.lang.Integer> found
這個語法
anotherArray[k]
// ^ ^
僅適用於數組類型。 沒有將anoterArray
聲明為數組。
您是要使用anArray
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.