Java_list using methods and four methods of traversing ArrayList

Source: Internet
Author: User

The 1.List interface provides a common method for itself that is related to the index, because the list collection is the type of the lists, the objects are stored linearly, and objects can be manipulated through the object's index.

The common implementation classes of the list interface are ArrayList and LinkedList, which, when using the list collection, are typically declared as a list type, instantiated as required by the actual situation, and instantiated as

ArrayList or LinkedList, e.g.:list<string> L = new arraylist<string&gt; ();//Instantiate List collection with ArrayList class

list<string> L2 = new linkedlist<string> (); //using the LinkedList class to instantiate the list collection  

2. The difference between the Add (int index, Object obj) method and the set (int index, Object obj) method requires attention when using the list collection to distinguish the Add (int index, Object obj) method and

Set (int index, Object obj) method,

The former is to add an object to the specified index location, which is the object that modifies the specified index position

The difference between the 3.indexOf (object obj) method and the LastIndexOf (object obj) method requires attention to distinguish between the IndexOf (object obj) method and the LastIndexOf (object obj) when using the list collection Method

The former is the smallest index position to get the specified object, and the latter is the largest index position for the specified object, provided that the specified object has duplicate objects in the list collection, otherwise if in the list collection

Has and has only one specified object, the index position obtained by these two methods is the same

The 4 sublist (int fromIndex, int toindex) method uses the sublist (int fromIndex, int toindex) method to intercept some objects in an existing list collection when generating a new list collection, it is important to note that

The newly generated collection contains the object represented by the starting index position, but does not contain the object represented by the terminating index position

Expand:

1. General usage:list< type > list=new arraylist< type > ();

2, list is an interface, can not be instantiated,

3, by instantiating its implementation class to use the list collection, His most commonly implemented class of ArrayList;

Use example:list<string> list= new arraylist<string> (); < data type string or int or boolen> indicates generic programming;

list<t> list=new arraylist<t> (); where type T is a constraint on the list collection element type,

For example, you declare a list<string> then add an object that is not of type string to this set. An exception is thrown.

Four ways to traverse ArrayList:
 Packagecom.test;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classArraylistdemo {publicstaticvoidMain (String args[]) {List<String> list = newarraylist<string>(); List.add ("Luojiahui"); List.add ("Luojiafeng"); //Method 1 IteratorIterator it1 =List.iterator ();  while(It1.hasnext ()) {System.out.println (It1.next ()); }       //Method 2 Principle and Method 1 the same        for(Iterator it2 =list.iterator (); It2.hasnext ();) {System.out.println (It2.next ()); }       //Method 3 is common, and does not use iterators     For  (String tmp:list) {SYSTEM.OUT.PRINTLN (TMP); }         //Methods 4 and C are very similar.        for(inti = 0;i < List.size (); i + +) {System.out.println (List.get (i)); }    }}

Java_list using methods and four methods of traversing ArrayList

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.