繁体   English   中英

发布Java通用类的实例化对象

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM