Java custom ArrayList sorting, skipped
There are two types of objects in a list. Now there is a need to sort the dates of different objects. below is the code
/*** @ Author Xiaohua xh1991101@163.com*/
List
Messages = messageSrv. getList (u. getUserId ());
List
MessageList = new ArrayList();For (Object o: messages ){MessageList. add (o );}For (Object o: chec
=elementdata.length;1.5 times times the original capacityint newcapacity = oldcapacity + (oldcapacity >> 1); if(Newcapacity-mincapacity ) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //mincapacity is usually close to size, so this is a win:Elementdata =arrays.copyof (Elementdata, newcapacity);}Private Static intHugecapacity (intmincapacity) { if(Mincapacity //Overflow Throw NewOutOfMemoryError (); return(Mincapacity > Max_
One practicepackage collection;import java.util.arraylist;import java.util.iterator;//Remove duplicate elements from ArrayList collection public Class Arraylisttest{public static void Sop (Object obj) {System.out.println (obj);} public static void Main (string[] args) {arraylist Java removes duplicate elements from the Arr
-Wxhm.put ("The Pride of the lake", array1); AWxhm.put ("The Condor Man", array2); + the //Add a collection of martial arts novels to the collection -Hm1.put ("Martial arts Fiction", WXHM); $ the //Traverse the the //get a collection of keys for a novel collection theSetHm1.keyset (); - //Traverse in for(String hm1key:hm1set) { the //the value of the novel collection is Hashmap theHashmapHm1.get (hm1key); About
Array
Array (array) and vector are very similar Java artifacts (constructs), which are completely different and should be determined according to their functions when choosing to use them.
1, array: Java arrays elements can not be subscript out of bounds, to a large extent, to ensure the security of Java programs, and other languages appear this problem often lea
The introduction of related packages, the package used here is net.sf.json-lib:json-lib:2.4, but the introduction of a bit of a pit, the package has two versions, jdk1.5 and jdk1.4, the introduction of the time to add the corresponding JDK version number, or not to introduce success.Gradle is used here, so add a line in the dependenciesCompile "net.sf.json-lib:json-lib:2.4:jdk15"If you write compile "net.sf.json-lib:json-lib:2.4", then you cannot introduce success.The following code shows how to
stored at the end of the array, and the insertion is inserted at any position.Pros and Cons Analysis:Advantages:
No additional storage space is added to represent the logical relationship between the data elements in the table;
You can quickly access elements from anywhere in the table.Disadvantages:
Insert and delete operations require a large number of elements to be moved;
When the length of linear table changes greatly, it is difficult to determine the capacity of stora
The following is an implementation of the ArrayList generic class. To avoid confusion with classes in the class library named myArrayList, the main details are:
The member variable contains the underlying array, the array capacity, and the current number of items stored in the myarraylist.
Provides a mechanism to change the capacity of the underlying array. By getting a new array, copy the old array to the new array to change the capacity
Object get (int index) {if (indexSystem.out.println ("Index exceeds the maximum index value or index is less than 0");return null;}return Elementdata[index];}Delete the value of the corresponding indexPublic Object Remove (int index) {if (Index System.out.println ("Index less than 0, or out of bounds");System.exit (0);}Object OldValue = this.get (index);System.arraycopy (elementdata, index + 1,elementdata, index, size);elementdata[size--] = null;return oldValue;}Pick up the values in the corres
Requirement: ArrayList stores custom objects and iterates through them. Requires generics to be joined and enhanced for traversal.A: iteratorsB: General forC: Enhanced forLinkedlist,vector,colleciton,list and other storage continue to practiceThe enhanced for is used to replace iterators.1 Packagecn_jdk5new;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 6 7 Public classArrListDemo2 {8 Public Static voidMain (string[] args) {9 //Create a
Define a person's class, save the ArrayList collection, remove the duplicate elements, define: The same name and age are the same as the repeating element.Take weightpublic class Qu {public static void Main (string[] args) {Sa2 a = new Sa2 ("Zhang San", 20);SA2 A1 = new Sa2 ("Zhang San", 30);SA2 a3 = new Sa2 ("Zhang San", 20);arraylistList.add (a);List.add (A1);List.add (A3);ArraylistSystem.out.println (List1);}public static arraylistArraylistListiter
Overview1, List features: linear sequential storage elements, elements can be duplicated, can be stored null2, ArrayList, LinkedListFirst, list featuresAs shown in this diagram, the list is stored in a linear structure , so it can be repeated or null. Of course, the specific sub-class is how to achieve, each has its own form.Second, the method of the listMost of these are not explained in the same way as the introduction of collection interfaces. Only
importjava.util.arraylist;importjava.util.linkedhashset;publicclassmainclass{ publicstaticvoidmain (String[]args) { //ConstructingAnArrayListArrayList Java to the elements in the ArrayList and the order of the same
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.