1.List Interface Overview
An ordered set of sequences. Users of this interface can have precise control over the insertion position of each element in the list. The user can access the element based on the index of the element (where it is in the list) and search for the elements in the list.
Unlike the set interface, a list usually allows repeating elements.
2.List case
Store strings and Traverse
Package Com;import java.util.arraylist;import java.util.iterator;import java.util.list;/** * Store string and traverse * */public class Listdemo {public static void main (string[] args) {//Create List Collection Object List<string> list = new arraylist<string> ();// add element List.add ("Hello"); List.add ("World"); List.add ("Java");//Traverse method One converts the collection to an object array and then outputs object[] obj = List.toarray (); for (int i = 0; i < obj.length; i++) {String str = (string) obj[i]; System.out.println (str);} Traversal method two uses iterators to iterate for (iterator<string> Iterator = List.iterator (); Iterator.hasnext ();) {String str = Iterator.next (); System.out.println (str);}}}
Store custom objects and traverse
package com;import java.util.arraylist;import java.util.iterator;import java.util.list;/** * stores custom objects and outputs */public class listdemo2 {public static void main (String[ ] args) {//Create List Collection Object List<student> list = new arraylist<student> (); /Create Student Object Student stu1 = new student ("Kwan-Yin Sister", 23); Student stu2 = new student ("The Tathagata elder brother", 46); Student stu3 = new student ("Chang ' e sister", 18);//Store the student object in the collection List.add (STU1); List.add (STU2); List.add ( STU3);//Traversal method one convert the collection to an array of  , and then Traverse Object[] obj = list.toarray ();for (int i = 0; i < obj.length; i++) {Student stu = (Student) obj[i]; System.out.println (stu);} Traversal method two iterate for by iterator for (Iterator<student> iterator = list.iterator (); Iterator.hasnext () ;) {student stu = iterator.next (); System.out.println (stu);}}}
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1862089
Java Collection Framework--list interface