1. Speak in code
1 PackageJavaproject;2 ImportJava.util.*;3 4 Public classa{5 Public Static voidMain (String[]args) {6List<string> list=NewArraylist<string> (arrays.aslist ("AB", "CD", "EF", "GH")));7 8 /**9 *list.add (param1), insert param1 at the end of the list,Ten *list.addall (param1), the list at the end of the list is inserted param1,param1 One *list.add (param1,param2), or List.addall (param1,param2), means inserting param1 inserts at param2 A *eg:list.add (0, "ij"); - *eg:list.addall (1,arrays.aslist ("KL", "MN")); - */ the - /** - *list.remove (0);//return list (0) - *list.remove (New String ("CD"));//return Boolean not found, return-1 + *list.removeall (arrays.aslist ("GH", "ef"));//return Boolean not found return-1 - */ + A /** at *list.set (0, "DKDKD");//Replace - */ - - /** - *list2.set (0, "ddddddd"); - *system.out.println (list);//[ddddddd, CD, EF, GH] in * Visible sublist function Maintainer reference to the original list object. - * So the manipulation of list2 will also be reflected in the list . to */ + - the * /** $ *list.get (param1);//Get param1 bit value, List.get (0);Panax Notoginseng *list.indexof (param2)//Get the serial number of the param2, List.indexof ("AB"); not found, return-1 - */ the + /** A * Here are two ways to sort the list and scramble the order the *collections.sort (list2); + *collections.shuffle (list2); - */ $ $ /** - *@paramCreate a clip from a larger list, front-open and closed - *list<string> list2=list.sublist (0,2); the */ - Wuyi /** the *!!! Note that although the Sublist function creates a fragment from a larger list, - * But the list behind the sublist () is the initial list, so any modifications to the returned list will Wu * React to the initial list and vice versa! - *list<string> list2=list.sublist (0,3); About *list<string> list3=list.sublist (2,3); $ *system.out.println (List2.retainall (LIST3))//throw Exception for unspecified reasons - * Probably because, list2,list3 the fragment of the list at the same time, list2, the removal operation caused List3 to be changed! - ================================================================= - *list<string> list2=list.sublist (0,3); A *list<string> list3=list.sublist (2,3); + *list2.remove (0); the *system.out.println (LIST3),//exception, where an exception is thrown to explain why the above Retainall throws an exception - */ $ the /** the The use of the Retainall function of *collection is to seek the intersection the JDK1.6 in the source code: the - Public Boolean Retainall (collection<?> c) { in Boolean modified = false; the iterator<e> E = Iterator (); the While (E.hasnext ()) { About if (!c.contains (E.next ())) { the E.remove (); the modified = true; the } + } - return modified; the }Bayi * So the meaning of this function is: the * Retain only those elements in the current collection that are also contained in the specified parameter collection the * If the current collection is changed because the function was called, it returns True, otherwise, false. - */ - the /** the * Whether to apply string constants directly, or to create a string object as an explicit new the * (the default Equals method for string variables is to return TRUE if the content is to be used) the * and the order, does not affect the results of Containsall - */ the the /** the *class pet{}94 * Note The default Equals method of the custom object, which is the same object as the comparison. the *list<pet> pets=new arraylist<pet> (); the *pets.add (New Pet ()); the *system.out.println (Pets.indexof (new Pet));//-1, Pets.contains (New Pet ()) False98 */ About - /**101 *list.toarray102 The *toarray method converts any collection to an array, which is an overloaded method103 * Its parameterless version returns an object array, such as String Str[]=list.toarray (); Type conversion error104 * If the data of the target type is passed to the overloaded version and can be checked by type, the data of the type is generated. the * Example: String []str=list.toarray (new string[0]);106 */107 /**108 * If the parameter array is too small to hold all the elements in the list, the ToArray () method will create an array of the appropriate size109 *string []str=new string[2]; the *str=list.toarray (new string[0]);111 *system.out.println (arrays.tostring (str)); the */113 //list.clear (); empty list the //list.size (); List size the //List.isEmpty (); Whether the list is empty the }117}
List interface method use (Ps:java programming thought reading summary)