Java Collection Framework--list interface

Source: Internet
Author: User

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 &nbsp, 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

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.