2

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?

1
  • I’d guess slice. Does Java have that? Or subList? Commented May 12, 2014 at 0:41

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Ooh, thanks for the warning. I really hate all this Java "by reference" crap.
@QuinnMcHugh Remember that you can construct a new list by passing the slice to the constructor. This will force a copy.
@Quinn: it's really a view not a clone. If you want to clone that slice, then you have to copy it into a new object. It's really not related to references.
@hexafraction I know, I' just prefer that I not have to say object = new Object(other). object = other, seems logical enough to me.
1

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.

Comments

0

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)

Comments

0

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]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.