Java 将 Arraylist 拆分为更小的 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20244401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:31:52  来源:igfitidea点击:

Java split Arraylist into smaller ArrayLists

javaarraylist

提问by Marc Rasmussen

I have an ArrayList with a size of 258

我有一个大小为的 ArrayList 258

Now i wish to split this into three different ArrayLists for this i have created the following code:

现在我希望将其拆分为三个不同的 ArrayLists,为此我创建了以下代码:

    Integer start = (int) Math.floor((sitesToSearch.size() / 3));
    Integer middle = (int) Math.floor((sitesToSearch.size() / 2));
    Integer end = (int) Math.floor((sitesToSearch.size() / 1));

    ArrayList<String> crawl_list1 = (ArrayList<String>)tmp.subList(0, start);
    ArrayList<String> crawl_list2 = (ArrayList<String>)tmp.subList(start+1, middle);
    ArrayList<String> crawl_list3 = (ArrayList<String>)tmp.subList(middle+1, end);

Sadly this throws the following error:

可悲的是,这会引发以下错误:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList$SubList cannot be cast to java.util.ArrayList

So how can i devide it into three smaller ArrayList

那么我怎样才能把它分成三个更小的 ArrayList

tmp declaration:

public ArrayList<String> getExternalLinks(ArrayList<String> rootDomains){
ArrayList<String> result = new ArrayList<String>();

Document doc = null;
/*
 * Check if root is valid
 * Find search the site for internal links
 */
for (String root : rootDomains) {
    if (!(root == null) || !root.isEmpty() ||!root.contains("#")) {
        try {
            doc = Jsoup.connect(root)
                    .userAgent("Mozilla")
                    .get();
            result.addAll(findExternalLinks(findInternalLinks(doc,root),root)); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
System.out.println(result.size());
return result;

}

}

采纳答案by Joachim Sauer

Simply use List<String>as the type of crawl_list1.

只需List<String>用作crawl_list1.

That's just an application of the general rule of "code against the interface".

这只是“针对接口的代码”一般规则的应用。

There's really no good reason to require the return value of subListto be an ArrayList(and it doesn't make sense as subListreturns a viewonto the original ArrayList).

确实没有充分的理由要求 的返回值subList是 an ArrayList(并且它没有意义,因为subList视图返回到原始ArrayList)。

If you absolutelyneed ArrayListobjects, then you need to copy the content into newArrayListobjects:

如果您绝对需要ArrayList对象,那么您需要将内容复制到ArrayList对象中:

ArrayList<String> crawl_list1 = new ArrayList<String>(tmp.subList(0, start));
ArrayList<String> crawl_list2 = new ArrayList<String>(tmp.subList(start+1, middle));
ArrayList<String> crawl_list3 = new ArrayList<String>(tmp.subList(middle+1, end));

回答by JB Nizet

Look at the javadocfor ArrayList.subList(). It doesn't return an ArrayList. It returns a List. There's no reason to cast this list to an ArrayList. It's a list, and that's all you need to know:

查看javadocArrayList.subList(). 它不会返回ArrayList. 它返回一个List. 没有理由将此列表转换为 ArrayList。这是一个列表,这就是您需要知道的全部内容:

List<String> crawl_list1 = tmp.subList(0, start);
List<String> crawl_list2 = tmp.subList(start+1, middle);
List<String> crawl_list3 = tmp.subList(middle+1, end);

Also, you should check your indices, because the end index passed to subList is exclusive.

此外,您应该检查您的索引,因为传递给 subList 的结束索引是独占的。

回答by Mason T.

.subList(fromIndex, toIndex) is inclusive for fromIndex and exclusive for toIndex. With the current code middle and start will never populate an ArrayList.

.subList(fromIndex, toIndex) 包含 fromIndex 和不包含 toIndex。使用当前代码 middle 和 start 永远不会填充 ArrayList。

The below two are assumptions of the logic:

以下两个是逻辑假设:

.subList(start, middle)

.subList(开始,中间)

.subList(middle, end+1)

.subList(中间,结束+1)