List function usage, List function usage

Source: Internet
Author: User

List function usage, List function usage

I. Basic Introduction

1. The elements in the List set are ordered and can be obtained by subscript.

2. Two common implementation classes of the List interface: ArrayList and List

3. Common Methods:

Get (), set (), add (), remove (), subList ()

4. Conversion between List and array:

A. Convert List to array:

B. Convert arrays to List:

/*** Convert List to array: toArray () method */List <String> list = new ArrayList <String> (); list. add ("one"); list. add ("two"); list. add ("three"); list. add ("four"); System. out. println ("original set:" + list); String [] array = list. toArray (new String [] {}); for (String str: array) {System. out. println (str);}/*** array to List: Arrays. asList () method */String [] array1 = {"five", "six", "seven", "eight"}; List <String> list1 = Arrays. asList (array1); System. out. println (list1 );}

5. List sorting:

(1) Natural sorting (from small to large ):

/*** Natural sorting of List */List <Integer> list = new ArrayList <Integer> (); Random ran = new Random (); for (int I = 0; I <10; I ++) {list. add (I, ran. nextInt (100);} System. out. println ("the original set is:" + list); Collections. sort (list); System. out. println ("the set after natural sorting is:" + list );

(2) sorting of reference types (two implementation methods ):

A. Comparable: The referenced element must be a subclass of Comparable.

Package Java20170330; import java. util. arrayList; import java. util. arrays; import java. util. collections; import java. util. list; class Point implements Comparable <Point> {int x; int y; public Point (int x, int y) {super (); this. x = x; this. y = y;} public String toString () {return "(" + x + "," + y + ")";} public int compareTo (Point p) {return (int) (Math. sqrt (this. x * this. x + this. y * this. y)-Math. sqrt (p. x * p. x-p.y * p. y) ;}} public class Blogtest01 {public static void main (String [] args) {List <Point> list = new ArrayList <Point> (); list. add (new Point (3, 3); list. add (new Point (1, 4); list. add (new Point (4, 3); list. add (new Point (2, 5); Collections. sort (list); System. out. println ("the sorted set is:" + list );}}



The running result is:
The sorted set is: [(), ()]

 

 

B. Comparator: applicable to temporary comparison rules

Demo1:

Package Java20170330; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; public class Blogtest {public static void main (String [] args) {List <String> list = new ArrayList <String> (); list. add ("cnmgb"); list. add ("nmb"); list. add ("nmgb"); list. add ("nm"); System. out. println ("the original set is:" + list); Collections. sort (list, new Comparator <String> () {public int compare (String str1, String str2) {return str1.length ()-str2.length () ;}}); System. out. println ("the sorted set is:" + list );}}



The running result is:

Original set: [cnmgb, nmb, nmgb, nm]
The sorted set is: [nm, nmb, nmgb, cnmgb]


 

Demo2:

Package Java20170330; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; class Point {int x; int y; public Point (int x, int y) {super (); this. x = x; this. y = y ;}@ Override public String toString () {return "(" + x + "," + y + ")";}} public class Blogtest {public static void main (String [] args) {List <Point> list = new ArrayList <Point> (); list. add (new Point (3, 3); list. add (new Point (1, 4); list. add (new Point (4, 3); list. add (new Point (2, 5); System. out. println ("the original set is:" + list); Collections. sort (list, new Comparator <Point> () {public int compare (Point p1, Point p2 ){
            return (int) (Math.sqrt(p1.x*p1.x+p1.y*p1.y)-Math.sqrt(p2.x*p2.x-p2.y*p2.y));
}
});
System. out. println ("sorted set:" + list );
}



The running result is:
Original set: [(), ()]
The sorted set is: [(), ()]

 

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.