简体   繁体   English

Java将Arraylist拆分为较小的ArrayList

[英]Java split Arraylist into smaller ArrayLists

I have an ArrayList with a size of 258 我有一个258大小的ArrayList

Now i wish to split this into three different ArrayLists for this i have created the following code: 现在,我希望将其拆分为三个不同的ArrayList,为此,我创建了以下代码:

    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;

} }

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 subList to be an ArrayList (and it doesn't make sense as subList returns a view onto the original ArrayList ). 确实没有充分的理由要求subList的返回值是ArrayList (并且由于subList视图返回到原始ArrayList上没有意义)。

If you absolutely need ArrayList objects, then you need to copy the content into new ArrayList objects: 如果绝对需要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));

Look at the javadoc for ArrayList.subList() . Javadoc中查看ArrayList.subList() It doesn't return an ArrayList . 它不返回ArrayList It returns a List . 它返回一个List There's no reason to cast this list to an ArrayList. 没有理由将此列表转换为ArrayList。 It's a list, and that's all you need to know: 这是一个列表,这就是您需要知道的所有内容:

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的末尾索引是互斥的。

.subList(fromIndex, toIndex) is inclusive for fromIndex and exclusive for toIndex. .subList(fromIndex,toIndex)对于fromIndex是包含的,对于toIndex是排除的。 With the current code middle and start will never populate an ArrayList. 使用当前代码,middle和start将永远不会填充ArrayList。

The below two are assumptions of the logic: 以下两个是逻辑假设:

.subList(start, middle) .subList(开始,中间)

.subList(middle, end+1) .subList(中间,尾+1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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