Java Foundation 16th Day: Remove duplicate content from the collection

Source: Internet
Author: User

As an example of storing strings in ArrayList, I do it myself before the instructor in the video lectures. The subject requirements are as follows

My own practice is to iterate through the collection with a for loop to get the elements in the collection. All elements after the current index are compared if they are identical and removed if the same, but after the code is complete, the implemented functionality is flawed, such as:

Find problems with debug, such as:

Modify the code in use after finding the problem

The first way to explain a teacher is to store the elements in the collection in a new collection. Iterate over the old collection with an iterator and then determine whether the new collection contains the collection element that will be the current iteration, and if it is not included, add it. Specific as

Later, the teacher also explained the method I used, reminding the students of the error-prone place. If not careful enough analysis, this is a very easy place to go wrong

Java Foundation 16th Day: Remove duplicate content from the collection

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.