Pregunta

In Java, and many other languages, one can grab a subsection of a string by saying something like String.substring(begin, end). My question is, Does there exist a built-in capability to do the same with Lists in Java that returns a sublist from the original?

¿Fue útil?

Solución

This method is called subList and exists for both array and linked lists. Beware that the list it returns is backed by the existing list so updating the original one will update the slice.

Otros consejos

The answer can be found in the List API: List#subList(int, int) (can't figure out how to get the link working....)

Be warned, though, that this is a view of the underlying list, so if you change the original list, you'll change the sublist, and the semantics of the sublist is undefined if you structurally modify the original list. So I suppose it isn't strictly what you're looking for...

If you want a structurally independent subsection of the list, I believe you'll have to do something like:

ArrayList<something> copy = new ArrayList<>(oldList.subsection(begin, end));

However, this will retain references to the original objects in the sublist. You'll probably have to manually clone everything if you want a completely new list.

The method is called sublist and can be found here in the javadocs

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#subList(int, int)

You can use subList(start, end)

ArrayList<String> arrl = new ArrayList<String>();
//adding elements to the end
arrl.add("First");
arrl.add("Second");
arrl.add("Third");
arrl.add("Random");
arrl.add("Click");
System.out.println("Actual ArrayList:"+arrl);
List<String> list = arrl.subList(2, 4);
System.out.println("Sub List: "+list);

Ouput :

Actual ArrayList:[First, Second, Third, Random, Click]
Sub List: [Third, Random]

You might just want to make a new method if you want it to be exactly like substring is to String.

public static List<String> sub(List<String> strs, int start, int end) {
    List<String> ret = new ArrayList<>(); //Make a new empty ArrayList with String values
    for (int i = start; i < end; i++) { //From start inclusive to end exclusive
        ret.add(strs.get(i)); //Append the value of strs at the current index to the end of ret 
    }
    return ret;
}

public static List<String> sub(List<String> strs, int start) {
    List<String> ret = new ArrayList<>(); //Make a new empty ArrayList with String values
    for (int i = start; i < strs.size(); i++) { //From start inclusive to the end of strs
        ret.add(strs.get(i)); //Append the value of strs at the current index to the end of ret 
    }
    return ret;
}    

If myStrings is an ArrayList of the following Strings: {"do","you","really","think","I","am","addicted","to","coding"}, then sub(myStrings,1,6) would return {"you", "really", "think", "I", "am"} and sub(myStrings,4) would return {"I", "am", "addicted", "to", "coding"}. Also by doing sub(myStrings, 0) it would rewrite myStrings as a new ArrayList which could help with referencing problems.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top