Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java split Arraylist into smaller ArrayLists

Tags:

java

arraylist

I have an ArrayList with a size of 258

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

    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

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;

}

like image 663
Marc Rasmussen Avatar asked Jan 26 '26 06:01

Marc Rasmussen


2 Answers

Simply use List<String> as the type of 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).

If you absolutely need ArrayList objects, then you need to copy the content into new ArrayList objects:

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));
like image 110
Joachim Sauer Avatar answered Jan 27 '26 19:01

Joachim Sauer


Look at the javadoc for 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:

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.

like image 31
JB Nizet Avatar answered Jan 27 '26 20:01

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!