Java Learning Lesson 44th-Collection Framework Tools Class (II)

Source: Internet
Author: User

Collections the tool class that controls the collection, arrays the tool class that manipulates the array

Most of the methods in arrays are overloaded

Focus on mastering aslist (); Converting arrays to collections

static
<T> List<T>
asList(T... a)
Returns a list of fixed sizes supported by the specified array.

Import Java.util.arrays;import Java.util.list;public class Main {public static void main (string[] args) {string[] str = { "E", "bn", "gr", "ASD",}; list<string> list = arrays.aslist (str); System.out.println (List.contains ("E")); List.add ("ASD");//Error System.out.println (List.size ());}}

Benefits:You can manipulate array elements using the methods in the collection

note that the array length is fixed, so the additions and deletions methods in the collection are not available, throwing exceptions

If an element in an array is an object, the elements in the array are stored directly as elements in the collection.

If the element in the array is a base type value, the array is stored as an element in the collection

Import Java.util.arraylist;import Java.util.arrays;import Java.util.list;public class Main {public static void main ( String[] args) {  int[] a = {1,2,3,4,5}; System.out.println (a.length); List<int[]> List2 = Arrays.aslist (a);//generics need to be written in array form System.out.println (List2.size ());}}

Set-to-go array

Using the ToArray method in the collection interface

Object[] toArray()
Returns an array that contains all the elements in this collection.
<T> T[]
toArray(T[] a)
Returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array.

The same set of variable groups can not be deleted

Import Java.util.arraylist;import Java.util.arrays;import Java.util.list;public class Main {public static void main ( String[] args) {  list<string > List = new arraylist<string> (); List.add ("ASD"); List.add ("EF"); list.add ("RG"); String[] strings = List.toarray (new string[5]); The ToArray method needs to pass in an array of the specified type//And if the length of the array is less than the size of the collection, the method creates a specified type with a length equal to a few size arrays//If it is larger than the size of the collection, the method uses the specified array to store the elements in the collection. Other locations default null SYSTEM.OUT.PRINTLN (Arrays.tostring (strings));}}

Recommendations,The specified length is the size of the collection


JDK 1.5 new features


Advanced for Loop

for (String:arr)

But the high-level for cannot traverse, the map sets such a double-column collection, you can change the map to a single set, and then use for

Import Java.util.hashmap;import Java.util.map;public class Main {public static void main (string[] args) {Map<integer, string> map = new Hashmap<integer, string> (), Map.put (1, "ASD"), Map.put (5, "ad"), Map.put (4, "SD"); for (Integer I : Map.keyset ()) {String va = map.get (i); System.out.print (i+ ":" +va+ "");} System.out.println ("------------------------"); for (Map.entry<integer, string> me:map.entrySet ()) { System.out.print (Me.getkey () + ":" +me.getvalue () + "");}}}

function Variable parameter

is actually an array, but the array element is received. and automatically adds these elements, simplifying the caller's writing

Note that the variable parameter type must be defined after the parameter list

Look up, that's how aslist used it.

Import Java.util.hashmap;import Java.util.map;public class Main {public static void main (string[] args) {int arr[] = { , 3,4,5,6,7,8,9};int sum = Add (arr); SYSTEM.OUT.PRINTLN (sum); int sum1 = Newadd (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); System.out.println (SUM1);} public static int Newadd (int ... arr,int a) error//public static int newadd (int a,int ... arr) correct public static int newadd (int: . ARR) {int sum = 0;for (int i = 0; i < arr.length; i++) {sum + = Arr[i];} return sum;} public static int Add (int a,int b,int c) {return a+b+c;} public static int Add (int[] arr) {int sum = 0;for (int i = 0; i < arr.length; i++) {sum + = Arr[i];} return sum;}}

Static Import

Static import with not much, although convenient a little, but the reading of a lot worse

Import Java.util.arraylist;import java.util.collections;import java.util.list;import static java.util.collections.sort;//static Import Import static java.util.collections.*;//imports collections all import static java.lang.system.*;//the same System can//actually import the static member of the class public class main {public static void main (string[] args) {list< string> list = new arraylist<string> (), List.add ("A4"), List.add ("A2"), List.add ("A1"), List.add ("A42"); SYSTEM.OUT.PRINTLN (list); Collections.sort (list); SYSTEM.OUT.PRINTLN (list), sort (list),//Do not need to write collectionsout.println (list) every time;}}


Java Learning Lesson 44th-Collection Framework Tools Class (II)

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.