Getting Started with Java Basics-iterator iterators and the use of set subscripts

Source: Internet
Author: User

Today, talk about the use of set iterator iterators and set subscripts


First on the code

Import Java.util.arraylist;import Java.util.iterator;public class Test {public Test () {//Initialize listadditemtolist ();} Private arraylist<string> list = new arraylist<string> ();p rivate void Additemtolist () {for (int i = 0; i < 5; i++) {List.add (i + "");}} Public arraylist<string> getList () {return list;} public void setlist (arraylist<string> list) {this.list = list;} public static void Main (string[] args) {Test test1 = new Test (); arraylist<string> test1list = Test1.getlist (); for (int i = 0; i < test1list.size (); i++) {//use subscript here, point directly to Element Test1li St.remove (i);} System.out.println ("Test1list has been removed all elements"); Test test2 = new test (); arraylist<string> test21list = Test2.getlist (); for (iterator<string> Iterator = Test21list.iterator (); Iterator.hasnext ();) {//Use iterators to set operations, you need to do next ()//Next () similar to a cursor, he is between two elements///Unlike the above directly pointing to an element Iterator.next (); Iterator.remove ();} System.out.println ("Test2list has been removed all elements");}}

Note the point:

1. when using iterators for collection operations, you need to do next ()

2.Next () is similar to a cursor, which is between two elements, and the subscript directly points to the element (see details)





Getting Started with Java Basics-iterator iterators and the use of set subscripts

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.