The Super big trap in the Java List.sublist method

Source: Internet
Author: User

Basic usage of sublist in ArrayList:

Sublist (Fromindex:int,toindex:int):list<e> Returns a sub-list from Fromindex to Toindex-1

In the use of the collection, you may often need to take a subset of the collection to do a bit of operation, so sublist this method into our eyes, not hesitate to use.

For example, the following code:

Public Static voidMain(Final String[]Args) {
List<Object>Lists= New ArrayList<Object> ();

Lists.Add("1");
Lists.Add("2");
Lists.Add("3");
Lists.Add("4");

List<Object>Templist=Lists.Sublist(2,Lists.Size());

Templist. "6"
system.. Printlntemplist//1

system.< Span class= "KWD" >out. Printlnlists//2
}

After the code is initially written, we may want to achieve the effect of adding an element 6 to the subset of the collection lists Templist, while the original collection remains unchanged.

This effect is reached: Lists = [1, 2, 3, 4],templist = [3, 4, 6]. But we see that the actual result is that the element 6 is also added to the lists inside.

What's going on here, by looking at the Java source code we can see: Templist's Sublist implementation code is inside the Abstractlist class, but anyway, The end result is to return a subclass of Abstractlist: Sublist (The class is a class decorated with the default modifier whose source code is located inside the Abstractlist.java class file).

How to construct the Sublist class:

Sublist(Abstractlist<E>List, IntFromIndex, IntToindex) {
If (FromIndex< 0)
Throw New Indexoutofboundsexception("FromIndex =" +FromIndex);
If (Toindex>List.Size())
Throw New Indexoutofboundsexception("Toindex =" +Toindex);
If (FromIndex>Toindex)
Throw New IllegalArgumentException("FromIndex (" +FromIndex+
+ Toindex +
L = List; offset = Fromindex; size = Toindex -;
Expectedmodcount = L.;
} /span>

Inside, the original list object is cached to a property of the Sublist class object.

In the method of modifying elements such as the add/remove of the Sublist class, L is used to do this:

Public voidAdd(IntIndex,E element) {
If (Index<0 ||Index>Size)
Throw new indexoutofboundsexception ();
Checkforcomodification (); L. (index+offset,< Span class= "PLN" > Element);
Expectedmodcount = L modcount; Size++; Modcount++; } /span>

Therefore, when we use subset Templist to modify an element, the existing list collection is affected. So when using the Sublist method, be sure to make sure that you need to modify the elements of the subcollections without affecting the existing list collection.

If you need to modify the elements of a sub-collection without affecting the original collection, we can use the following methods to handle it:

Public Static voidMain(Final String[]Args) Public Static voidMain(Final String[]Args) {
List<Object>Lists= New ArrayList<Object> ();

Lists.Add("1");
Lists.Add("2");
Lists.Add("3");
Lists.Add("4");

Note that this is different from the code at the top of this article ....
List<Object>Templist= New ArrayList<Object> (Lists.Sublist(2,Lists.Size()));

Templist. "6"
system.. Printlntemplist//1

system.< Span class= "KWD" >out. Printlnlists//2
}

Super-Big traps in Java list.sublist methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.