In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the pit when deleting the list collection in java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the pit when deleting the list collection in java"!
About the pit when the list collection does the delete operation
What is the problem with an ArrayList if you use the following ways to delete it?
Import java.util.ArrayList;import java.util.List; public class DeleteList {public static void main (String [] args) {List list = new ArrayList (); list.add (1); list.add (2); list.add (3); list.add (4); for (int iposit I = size) throw new NoSuchElementException (); Object [] elementData = ArrayList.this.elementData If (I > = elementData.length) throw new ConcurrentModificationException (); cursor = I + 1; return (E) elementData [lastRet = I];} public void remove () {if (lastRet < 0) throw new IllegalStateException (); checkForComodification () Try {ArrayList.this.remove (lastRet); cursor = lastRet; lastRet =-1; expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException ();} Common operations on List collections
Note: the code in the content is relevant.
Add, get, delete elements in 1.list
Add method: .add (e)
Get method: .get (index)
Delete method: .remove (index)
Delete by index; .remove (Object o)
Delete by element content
List person=new ArrayList (); person.add ("jackie"); / / Index is 0 / .add (e) person.add ("peter"); / / Index is 1 person.add ("annie"); / / Index is 2 person.add ("martin"); / / Index is 3 person.add ("marry") / / Index is 4 person.remove (3); / / .remove (index) person.remove ("marry"); / / .remove (Object o) String per= ""; per=person.get (1); System.out.println (per) / .get (index) for (int I = 0; I < person.size (); iTunes +) {System.out.println (person.get (I)); / / .get (index)} whether the 2.list contains an element
Method: .resume (Object o); returns true or false
List fruits=new ArrayList (); fruits.add ("apple"); fruits.add ("banana"); fruits.add ("peach"); / / for loop traversing list for (int I = 0; I < fruits.size (); iTunes +) {System.out.println (fruits.get (I)) } String appleString= "Apple"; / / true or false System.out.println ("does fruits include Apple:" + fruits.contains (appleString)); if (fruits.contains (appleString)) {System.out.println ("I like apples") } else {System.out.println ("I am unhappy");} change the element value according to the index in 3.list (replace)
Note the difference between .set (index, element); and .add (index, element);
String a = "Bailongma", b = "Sha Wujing", c = "Bajie", d = "Tang monk", e = "Wukong"; List people=new ArrayList (); people.add (a); people.add (b); people.add (c); people.set (0, d); / / .set (index, element) / / place d Tang monk in list with index 0, and replace a Bailongma people.add (1, e); / / .add (index, element) / / put e Wukong to the position with index 1 in list, move the original b Sha Wujing back one bit / / enhance the for loop to traverse list for (String str:people) {System.out.println (str);} view (judge) the index of the element in 4.list
Note: the difference between .indexOf () and lastIndexOf ()
List names=new ArrayList (); names.add ("Liu Bei"); / / Index is 0 names.add ("Guan Yu"); / / Index is 1 names.add ("Zhang Fei"); / / Index is 2 names.add ("Liu Bei"); / / Index is 3 names.add ("Zhang Fei") / / the index is 4 System.out.println (names.indexOf ("Liu Bei"); System.out.println (names.lastIndexOf ("Liu Bei")); System.out.println (names.indexOf ("Zhang Fei")); System.out.println (names.lastIndexOf ("Zhang Fei")); 5. If (names.indexOf ("Liu Bei") = = 0) {System.out.println ("Liu Bei");} else if (names.lastIndexOf ("Liu Bei") = = 3) {System.out.println ("Liu Bei is there");} else {System.out.println ("where is Liu Bei?") 6. Regenerate a new list (intercept collection) using the index location in list
Methods:
.subList (fromIndex, toIndex)
Size () this method gets the sum of the number of elements in list
List phone=new ArrayList (); phone.add ("Samsung"); / / Index is 0 phone.add ("Apple"); / / Index is 1 phone.add ("Hammer"); / / Index is 2 phone.add ("Huawei"); / / Index is 3 phone.add ("Xiaomi") / / Index 4 / / the original list traverses for (String pho:phone) {System.out.println (pho);} / / generates a new list phone=phone.subList (1,4) / / .sublist (fromIndex, toIndex) / / rebuilds a list using objects with indexes 1-4, but does not contain elements with index 4, 4-1 phone.size 3 for (int I = 0; I < phone.size ()) {/ / phone.size () this method gets the sum of the number of elements in list ("the new list contains elements" + phone.get (I)) } 7. Compare all the elements in the two list
The equals method of two equal objects must be true, but two objects with equal hashcode are not necessarily equal objects.
/ / 1.
If (person.equals (fruits)) {System.out.println ("all elements in the two list are the same");} else {System.out.println ("all elements in the two list are different");} / 2. If (person.hashCode () = = fruits.hashCode ()) {System.out.println ("We are the same");} else {System.out.println ("We are different");} 8. Determine whether the list is empty
/ / returns true if it is empty, and false if it is not empty.
If (person.isEmpty ()) {System.out.println ("empty");} else {System.out.println ("not empty");} 9. Returns the Iterator collection object System.out.println ("return Iterator collection object:" + person.iterator ()); 10. Convert the collection to the string String liString= "; liString=person.toString (); System.out.println (" convert the collection to a string: "+ liString); 11. Convert the collection to an array System.out.println ("convert the collection to an array:" + person.toArray ()); 12. Collection type conversion / / 1. The default type List listsStrings=new ArrayList (); for (int I = 0; I < person.size (); iTunes +) {listsStrings.add (person.get (I));} / / 2. Specify type List lst=new ArrayList (); for (String string:person) {lst.add (StringBuffer (string));} 13. To repeat List lst1=new ArrayList (); lst1.add ("aa"); lst1.add ("dd"); lst1.add ("ss"); lst1.add ("aa"); lst1.add ("ss"); / / method 1. For (int I = 0; I I ) {if (lst1.get (j) .equals (lst1.get (I) {lst1.remove (j);} System.out.println (lst1); / / method 2. List lst2=new ArrayList () For (String s:lst1) {if (Collections.frequency (lst2, s)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.