Collection Framework (Unique features overview and testing of the list collection)

Source: Internet
Author: User

Package cn.itcast_03;


Import java.util.ArrayList;

Import java.util.List;


/*

* Unique features of the list collection:

* A: Add features

* void add(int index,objectElement): adding elements at the specified location

* B: Get features

* Object get(int index): gets The element at the specified position

* C: List iterator

* listiterator listiterator():List Collection-specific iterator

* D: delete function

* Object Remove (int index): Deletes the element according to the index, returns the element that was deleted , ( To delete you and return you.)

* E: Modify function

* Object set(int index,objectElement): modifies elements according to index, returns modified elements

*/

public class Listdemo {

public static void Main (string[] args) {

To Create a Collection object

List List = new ArrayList ();


adding elements

List.add ("Hello");

List.add ("World");

List.add ("Java");


void Add (int index,object Element): add element at specified position

List.add (1, "Android");//No problem


indexoutofboundsexception( index out of bounds)

List.add (One, "Java ee");//problem


List.add (3, "Java ee"); No problem

List.add (4, "Java ee"); Have a problem


Object get (int index): Gets the element at the specified position

System.out.println ("Get:" + list.get (1));


Indexoutofboundsexception (index out of bounds)

System.out.println ("Get:" + list.get (11));


Object Remove (int index): Deletes elements based on index, returns deleted elements

System.out.println ("Remove:" + list.remove (1));


Indexoutofboundsexception (index out of bounds)

System.out.println ("Remove:" + list.remove (11));


Object set (int index,object element): Modifies the element according to the index, returning the modified element

System.out.println ("set:" + list.set (1, "Java ee")); The index is starting at 0 , which means that index 0 represents the Helloin the list collection, and the result returned by this method is the world (the modified element) the element at this time is Hello,javaee,java


System.out.println ("list:" + list);

}

}


This article is from the "GD" blog, please be sure to keep this source http://wangdenghui.blog.51cto.com/9930072/1768739

Collection Framework (Unique features overview and testing of the list 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.