Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将泛型列表转换为泛型数组_Java_Arrays_Generics_Linked List - Fatal编程技术网

Java 将泛型列表转换为泛型数组

Java 将泛型列表转换为泛型数组,java,arrays,generics,linked-list,Java,Arrays,Generics,Linked List,如何将列表中的项转换为CountedItem[] import java.util.LinkedList; import java.util.Collections; import java.util.Comparator; public class CountedSet<E> { LinkedList<CountedItem<E>> items = new LinkedList<CountedItem<E>>();

如何将列表中的
转换为
CountedItem[]

import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;

public class CountedSet<E>
{
    LinkedList<CountedItem<E>> items = new LinkedList<CountedItem<E>>();
    private final Comparator<CountedItem<E>> comparator = new Comparator<CountedItem<E>>()
    {
        @Override
        public int compare(final CountedItem<E> o1, final CountedItem<E> o2)
        {
            return Integer.valueOf(o1.getCount()).compareTo(Integer.valueOf(o2.getCount()));
        }
    };

    public CountedSet()
    {
    }

    public CountedItem<E> add(E item)
    {
        CountedItem<E> currentItem = null;
        for (CountedItem<E> listItem : items)
            if (listItem.getItem().equals(item))
            {
                currentItem = listItem;
                break;
            }

        if (currentItem != null)
            currentItem.increment();
        else
        {
            currentItem = new CountedItem<E>(item);
            items.add(currentItem);
        }

        return currentItem;
    }

    public CountedItem<E>[] getSortedArray()
    {
        Collections.sort(items, this.comparator);

        CountedItem<E>[] array = items.toArraySomeHow();
        return array;
    }
}
import java.util.LinkedList;
导入java.util.Collections;
导入java.util.Comparator;
公共类计数集
{
LinkedList items=新建LinkedList();
专用最终比较器比较器=新比较器()
{
@凌驾
公共整数比较(最终CountedItem o1,最终CountedItem o2)
{
返回Integer.valueOf(o1.getCount()).compareTo(Integer.valueOf(o2.getCount());
}
};
公共计数集()
{
}
公共CountedItem添加(E项)
{
CountedItem currentItem=null;
对于(CountedItem listItem:items)
if(listItem.getItem().equals(item))
{
currentItem=listItem;
打破
}
如果(currentItem!=null)
currentItem.increment();
其他的
{
currentItem=新CountedItem(项目);
项目。添加(当前项目);
}
返回当前项目;
}
公共CountedItem[]GetSorterDarray()
{
Collections.sort(items,this.comparator);
CountedItem[]数组=items.toArraySomeHow();
返回数组;
}
}
我没有太多的细节要告诉你,除了我想在
public CountedItem[]getSortedArray()
(最后一个方法)中完成这项工作之外。此外,这样做会导致错误:

CountedItem<E>[] array = items.toArray(new CountedItem<E>[items.size()]);
     // ERROR - Cannot create a generic array of CountedItem<E>. ^
CountedItem[]array=items.toArray(新的CountedItem[items.size());
//错误-无法创建CountedItem的常规数组^

您可以使用
items.toArray(新CountedItem[items.size())带有警告。但是,修改后的代码不干净,类型不安全

您还可以添加
@SuppressWarnings(“未选中”)
以消除警告


这取决于你是否能接受它:)

你可以使用
items.toArray(new CountedItem[items.size())带有警告。但是,修改后的代码不干净,类型不安全

您还可以添加
@SuppressWarnings(“未选中”)
以消除警告


这取决于你是否能接受它:)

我不相信你能做一般数组,但你可以做一般数组列表。这可能会有所帮助,具体取决于您想要数组的原因(在链表上的速度?)。ArrayList在内部实现为
对象[]
。也许是这样的

public CountedItem<E>[] getSortedArray()
{
    Collections.sort(items, this.comparator);

    return new ArrayList<E>(items);
}
public CountedItem[]getSortedArray()
{
Collections.sort(items,this.comparator);
返回新的ArrayList(项目);
}

我不相信你能做一般数组,但你能做一般数组列表。这可能会有所帮助,具体取决于您想要数组的原因(在链表上的速度?)。ArrayList在内部实现为
对象[]
。也许是这样的

public CountedItem<E>[] getSortedArray()
{
    Collections.sort(items, this.comparator);

    return new ArrayList<E>(items);
}
public CountedItem[]getSortedArray()
{
Collections.sort(items,this.comparator);
返回新的ArrayList(项目);
}

java不支持通用数组,无法解决此问题?您可以使用
items.toArray(new CountedItem[items.size())带有warning@hoaz +1. 这很有效,是我见过的最简单的解决方案。。。这是否合适?java不支持泛型数组,没有办法解决这个问题?您可以使用
items.toArray(new CountedItem[items.size())带有warning@hoaz +1. 这很有效,是我见过的最简单的解决方案。。。但这是正确的吗?