List Collection Class

Source: Internet
Author: User

1.1:list.add Method--Add an object to the list of collections

public static void Main (string[] args) {

List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
By looping through the contents of the list
for (int i=0;i<list.size (); i++) {
System.out.println (i+ ":" +list.get (i));
}
}

1.1.1:

void Add (int index, E Element)

Index: Used to specify the indexes in which to insert the specified element.

element: Used to specify which elements to insert.

The index value starts at 0.

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
List.add (1, "Starting from Me"); Add data at the location of the 1th + 1 element
By looping through the contents of the list
for (int i=0;i<list.size (); i++) {
System.out.println (i+ ":" +list.get (i));
}
}

1.2:list.addall Method--Add all elements to the list

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
List.add (1, "Starting from Me"); Add data at the location of the 1th + 1 element
List<string> list_ad=new arraylist<string> ();
List_ad.add ("Public service advertisement");
Add all the elements in the list to the List_ad
System.out.println ("Add Succeeded:" +list_ad.addall (list));
By looping through the contents of the list
for (int i=0;i<list_ad.size (); i++) {
System.out.println (i+ ":" +list_ad.get (i));
}
}

1.2.1:

public static void Main (string[] args) {

List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
List<string> list_ad=new arraylist<string> ();
List_ad.add ("Public service advertisement");
List_ad.add ("Thank you for watching");
Add all the elements in the list to the List_ad
System.out.println ("Add Succeeded:" +list_ad.addall (1,list));
By looping through the contents of the list
for (int i=0;i<list_ad.size (); i++) {
System.out.println (i+ ":" +list_ad.get (i));
}

}

1.3 List.clear Method--Remove all elements from the list

public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
SYSTEM.OUT.PRINTLN ("Data in the list collection before using the Clear method");
for (int i = 0; i < list.size (); i++) {//by looping through the contents of the list
SYSTEM.OUT.PRINTLN (i + ":" + list.get (i));
}
System.out.println ("After using the clear method, the data in the list collection");
List.clear (); Remove all elements
if (list.size () <= 0) {
System.out.println ("The list object is empty! ");
}
}

1.4 List.contains Method--Determines whether the list contains the specified element

public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Strawberry"); Add data to the list
List.add ("banana"); Add data to the list
List.add ("pineapple"); Add data to the list
for (int i = 0; i < list.size (); i++) {//by looping through the contents of the list
SYSTEM.OUT.PRINTLN (i + ":" + list.get (i));
}
String o = "Apple";
System.out.println ("The list object contains elements" + O + ":" + list.contains (o));
Determines whether the string contains the specified string object
}

1.5 List.get Method--Gets the element at the specified position in the list

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
String Ret=list.get (1);
System.out.println ("Gets the element with index position 1:" +ret);
}

1.6 List.isEmpty Method--Judging whether the collection object is empty

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Starting from Me"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
Boolean empty=list.isempty ();
if (empty) {
System.out.println ("The list is empty");
}else{
System.out.println ("The list is not empty");
}
List.clear (); Remove all elements from the list
SYSTEM.OUT.PRINTLN ("After executing the Clear method");
Empty=list.isempty ();
if (empty) {
System.out.println ("The list is empty");
}else{
System.out.println ("The list is not empty");
}
}

1.7 List.iterator Method--Iterate over list elements

public static void Main (string[] args) {
List<string> list=new arraylist<string> (); Defining a List Collection object
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
Iterator It=list.iterator (); Get Iterator Object
while (It.hasnext ()) {//loops through iterator objects
System.out.println (It.next ()); Output the value in the Iterator object
}
}

1.8 List.remove Method--Removes the specified element from the list

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Apple"); Add data to the list
List.add ("Strawberry"); Add data to the list
List.add ("banana"); Add data to the list
String Str=list.remove (1); Remove an element with an index position of 1
System.out.println ("I do not like to eat fruit is:" +str);
Iterator It=list.iterator (); Gets the iterator object for the collection
System.out.println ("The fruit that loves to eat is:");
while (It.hasnext ()) {//Traverse iterator Object
System.out.println (It.next ()); Output iterator elements in an object
}
}

1.9 List.removeall Method--Remove all elements from the list

public static void Main (string[] args) {
  List<String> List = new arraylist<string> ();
  list.add ("Protect the Environment");      //Add data to the list
  list.add ("Love the Earth");       //Add data to the list
  list.add ("Start from Me");       //Add data to the list
  List<String> list1 = new arraylist<string> ();
  list1.add ("Environmental protection");       //Add data to the list
  list1.add ("Love the Earth");       //Add data to the list
  boolean ret = List.removeall (list1);   //Remove the same element as List1 from the list
  Iterator<String> it = list.iterator ();  //Create iterator
  while (It.hasnext ()) {       //loop-through iterator
   system.out.println (It.next ());    //Output set element
  }
}

Run the program, remove the contents of the List1 from the collection list, and iterate through the collection list, resulting in "starting from Me".

2.0 List.size Method--Returns the number of elements in a list

public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Environmental protection"); Add data to the list
List.add ("Love the Earth"); Add data to the list
List.add ("Starting from Me"); Add data to the list
int listsize=list.size (); Get the number of elements in a list
System.out.println ("Number of elements in the list:" +listsize);
}

Run the program, and the number of elements in the output list is 3.

2.1 List.sublist Method--Gets the child list of the specified range in the list

sublist (int fromIndex, int toindex)

FromIndex: Used to specify the starting point (including the point) of the new list.

Toindex: Used to specify the end point of the new list (excluding that point).

public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Javaweb Programming Dictionary"); Add data to the list
List.add ("Java Programming dictionary"); Add data to the list
List.add ("C # Programming Dictionary"); Add data to the list
List.add ("ASP.        NET programming dictionary "); Add data to the list
List.add ("VC Programming Dictionary"); Add data to the list
List.add ("SQL Programming Dictionary"); Add data to the list
iterator<string> its = List.iterator (); Get Collection iterator
SYSTEM.OUT.PRINTLN ("All element objects in the collection:");
while (Its.hasnext ()) {//loops through the collection
System.out.print (Its.next () + ""); Output Collection Contents
}
list<string> sublist = List.sublist (3, 5); Get child list
System.out.println ("\ n intercept some elements of the collection:");
Iterator it = Sublist.iterator ();
while (It.hasnext ()) {
System.out.print (It.next () + "");
}
}

List Collection Class

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.