Java list Interface Usage and javalist Interface Usage

Source: Internet
Author: User

Java list Interface Usage and javalist Interface Usage

List is an interface that inherits from the collection interface. The type of values stored in list is declared by list <object> generic type. When storing values, the add method of the ArrayList class is called, you can call the remove Method When deleting a file. Many methods of list are similar to set, but there are differences in the storage structure. set storage features unordered and different. On the contrary, list storage features ordered and stored differently.

 

Package test_list; import java. util. arrayList; import java. util. hashSet; import java. util. iterator; import java. util. list; import java. util. set;/*** @ author Qiaofengbo * @ time July 21, 2014 */public class Test_list {static List <String> lists1 = new ArrayList <String> (); static Set <String> sets1 = new HashSet <> (); public static void main (String args []) {save_to_list (); bianli_lists1 (); // bianli_lists1_iterator (); loadElementsToSet (lists1); bianli_sets1 ();}/*** store the value in the list */public static void save_to_list () {lists1.add ("1 "); lists1.add ("2"); lists1.add ("3"); lists1.add (""); lists1.add ("3"); lists1.add ("4 "); lists1.add ("3"); lists1.add (""); lists1.add ("4");}/*** traverse list */public static void bianli_lists1 () {for (String l1: lists1) {System. out. print (l1 + "\ t");} System. out. print ("\ n");}/*** use iterator to traverse list */public static void bianli_lists1_iterator () {Iterator <String> ite = lists1.iterator (); while (ite. hasNext () {System. out. print (ite. next () + "\ t ");}} /*** load the values in the list into the set */public static Set <String> loadElementsToSet (List <String> list) {for (int I = 0; I <list. size (); I ++) {for (int j = I + 1; j <list. size (); j ++) {if (list. get (I ). equals (list. get (j) {sets1.add (list. get (I); continue ;}}return sets1;}/*** traverse set */public static void bianli_sets1 () {for (String s1: sets1) {System. out. print (s1 + "\ t ");}}}

 

Interview Questions:

Find the repeated elements in the list?
Analysis: because you do not know the number of repeated elements in the list, the unique elements in the list are put into the set container by using the set container's unique feature, finally, traverse the set container to find the repeated elements in the list. See Methods loadElementsToSet and sets1.


What is the list in java? What is the interface?

List is a number that can change the length at will.
List itself can only be used as the parent class reference of ArrayList and rule list.
List l = new ArrayList ();
Use this method

Why can I use the iterator () method of the JAVA interface List directly?

Take a look at be. getBrand (BearColor). it returns the object that has implemented the iterator () method, so you can Iterator it = result. iterator ();

Related Article

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.