将泛型Arraylist转换为具有泛型类型的数组

时间:2022-03-21 16:09:27

I'm trying to use ArrayList's built in Java method that converts an ArrayList to an array. However, the issue is that Java converts the array to an array of type objects. I am trying to convert this to an array of type T (generic type). I tried passing in the class type into the constructor of the class using it to cast the array, but I had no luck. My code is below. Any help would be appreciated:

我正在尝试使用ArrayList的内置Java方法将ArrayList转换为数组。但是,问题是Java将数组转换为类型对象数组。我正在尝试将它转换为类型为T(泛型类型)的数组。我尝试将类类型传递给类的构造函数,使用它来转换数组,但是我没有运气。我的代码如下。如有任何帮助,我们将不胜感激:

public class QuickSelect> extends Sort implements Selection {

公共类QuickSelect>扩展了排序实现了选择{

Class<T> t; // for class type 

QuickSelect(Class<T> t){
    this.t = t;
}

@Override
public T select(T[] data, int n, int k) {

    if(data.length == 0) return null;
    if(k == 1) return data[0];
    if(k >= n || k <=0 ) return null;

    Random randomGenerator = new Random();

    int pivotPosition = randomGenerator.nextInt(n-1);
    T pivotValue = data[pivotPosition];

    ArrayList<T> lessThanPivot = new ArrayList<T>();
    ArrayList<T> equalToPivot = new ArrayList<T>();
    ArrayList<T> greatThanPivot = new ArrayList<T>();

    for(int i=0; i < n; i++){
        if(compare(pivotValue, data[i]) < 0) lessThanPivot.add(data[i]);
        else if(compare(pivotValue, data[i]) == 0) equalToPivot.add(data[i]);
        else greatThanPivot.add(data[i]);

    }
    Class<?> tClass = t.getClass();

    if(k <= lessThanPivot.size()) select(lessThanPivot.toArray(), lessThanPivot.size(), k); // this part of the code is where the issue is

    return null; //don't worry about this for now
    }

}

2 个解决方案

#1


2  

There are two versions of toArray() methods in ArrayList - the one that gets no argumnets, and returns array of Object (that you use) and the other one, that gets as argument the type of array you want to it to return: public <T> T[] toArray(T[] a)

ArrayList中有两个版本的toArray()方法——一个没有argumnets,返回你使用的对象数组;另一个,作为你想要返回的数组类型:public T[] toArray(T[] a)

so you need to use this one, like this: lessThanPivot.toArray(data)

需要使用这个,比如:lessthanpivot。toarray(数据)

see full javadoc here

看到完整的javadoc

#2


2  

If i got your question right i sugget you to use List.toArray(T[])

如果我答对了你的问题,我建议你用List.toArray(T[])

Example String[]array = list.toArray(new String[list.size()]);

例如String[]数组列表=。toArray(新的字符串(list.size()));

Generics are removed when code is compiled, thus the helper.array will just return an object[].

在编译代码时,将删除泛型,从而删除帮助程序。数组只返回一个对象[]。

I hope this will help.

我希望这能有所帮助。

#1


2  

There are two versions of toArray() methods in ArrayList - the one that gets no argumnets, and returns array of Object (that you use) and the other one, that gets as argument the type of array you want to it to return: public <T> T[] toArray(T[] a)

ArrayList中有两个版本的toArray()方法——一个没有argumnets,返回你使用的对象数组;另一个,作为你想要返回的数组类型:public T[] toArray(T[] a)

so you need to use this one, like this: lessThanPivot.toArray(data)

需要使用这个,比如:lessthanpivot。toarray(数据)

see full javadoc here

看到完整的javadoc

#2


2  

If i got your question right i sugget you to use List.toArray(T[])

如果我答对了你的问题,我建议你用List.toArray(T[])

Example String[]array = list.toArray(new String[list.size()]);

例如String[]数组列表=。toArray(新的字符串(list.size()));

Generics are removed when code is compiled, thus the helper.array will just return an object[].

在编译代码时,将删除泛型,从而删除帮助程序。数组只返回一个对象[]。

I hope this will help.

我希望这能有所帮助。