Common methods of the list interface in the collection interface in the collection class in Java are familiar with

Source: Internet
Author: User
Tags set set

1: Collection class, the Java.util package in the Java language provides some collection classes, which are also known as containers.

2: Distinguish set classes and arrays. (1) The length of the array is fixed, and the length of the set is variable. (2) An array is used to hold the base data type, and the collection is a reference to hold the object.

3: Common collections have list sets, set sets, and map collections. Where the list collection and set set implement the collection interface.

The 4:collection interface is the root interface in the hierarchy that forms the collection unit, which is called the element. The collection interface is not typically used directly, but the interface provides methods for adding and removing elements and managing data. Because both the list interface and the set interface implement the collection interface, these methods are common to the list collection and set set.

The 5:list collection is a list type that stores objects in a linear manner so that objects can be manipulated through the object's index.

The Add (in Index,object obj) method in the 6:list collection, which is used to add an object to the specified index position in the collection, where the index position of the collection starts at 0 and the index position of the other object moves backward one bit.

The set (int index,e Element) method in the 7:list collection replaces the element at the specified position in the list with the specified element, returning the element that was previously at the specified position.

The case is as follows, creating a collection object, adding elements to the collection, modifying the elements in the collection through the set method, and adding elements to the collection through the Add () method, all iterating through the collection elements.

1  Packagecom.ning;2 3 ImportJava.util.*;4 5 /**6  * 7  * @authorBiexiansheng8  *9  */Ten  Public classDemo { One  A      Public Static voidMain (string[] args) { -String a= "A", b= "B", c= "C", d= "D", e= "E";//defines a string object to insert into the collection -List<string> list=NewLinkedlist<string> ();//Create a list collection theList.add (a);//adding elements to the collection - List.add (b); - List.add (c); -Iterator<string> Iter=list.iterator ();//to create an iterator for a collection +System.out.println ("The elements in the pre-modified collection are:"); -          while(Iter.hasnext ()) { +System.out.print (Iter.next () + ""); A         } atList.set (0,e);//modifies an object with an index position of 0 to an object e -List.set (2,D);//modify an object with an index position of 2 to object D -Iterator<string> It=list.iterator ();//to create an iterator object that modifies the collection object - System.out.println (); -SYSTEM.OUT.PRINTLN ("the element in the modified collection is:"); -          while(It.hasnext ()) {//Loop gets the elements in the collection inSystem.out.print (It.next () + ""); -         } to      +          -     } the      *}

1  Packagecom.ning;2 3 ImportJava.util.*;4 5  Public classDemo01 {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9String a= "A", b= "B", c= "C", d= "D", e= "E", apple= "Apple";//objects to add to the collectionTenList<string> list=NewArraylist<string> ();//To Create a list collection Object OneList.add (a);//object A has an index position of 0 AList.add (Apple);//Object Apple has an index position of 1 - List.add (b); - List.add (apple); the List.add (c); - List.add (apple); - List.add (d); -List.remove (1);//Remove (int index) removes an object from the specified index in the collection +System.out.println ("Elements in the ArrayList collection:" +list); -System.out.println ("Apple first appears in index position:" +List.indexof (apple)); +System.out.println ("Apple last seen index location:" +List.lastindexof (apple)); ASystem.out.println ("B first occurrence index position:" +List.indexof (b)); atSystem.out.println ("B last seen index position:" +List.lastindexof (b)); -System.out.println ("***********************************"); -         //System.out.println ("Get () gets the object at the specified index position:" +list.get (0)); -          -          -     } in  -}

The results of this two sentences comparison show:

List.remove (1);//remove (int index) is used to remove an object from the specified index position in the collection

System.out.println ("Get () gets the object at the specified index position:" +list.get (0));

At this point, the collection interface under the list interface learning is almost, detailed also please yourself down to the reference, practice, proficiency in the application and mastery.

Common methods of the list interface in the collection interface in the collection class in Java are familiar with

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.