Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java delete eligible data from list

Shulou Source: shulou.com Published: 2022-06-02 14:34:46 11月16日 Update

This article mainly introduces how Java deletes qualified data from list, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Delete eligible data from list

In the use of the list language, you often encounter the need to remove some data from Java, but beginners often encounter the following pits:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4

At java.util.ArrayList.rangeCheck (Unknown Source)

At java.util.ArrayList.get (Unknown Source)

At Main.remove3 (Main.java:44)

At Main.main (Main.java:18)

So here are some ways to delete data from list:

Import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Main {public static void main (String [] args) {ArrayList strs = new ArrayList (); strs.add ("1"); strs.add ("32"); strs.add ("3"); strs.add ("4"); strs.add ("5"); strs.add ("36") / / remove1 (strs); / / remove2 (strs); / / remove3 (strs); remove4 (strs); System.out.println ("after"); printList (strs);} / / use iterator, which is a method often used in java and Android source code, so public static void remove1 (List list) {Iterator sListIterator = list.iterator () is most recommended While (sListIterator.hasNext ()) {String str = sListIterator.next (); if (str.contains ("3")) {sListIterator.remove () Delete in reverse order to prevent data error public static void remove2 (List list) {for (int I = list.size ()-1; I > = 0; iMel -) {if (list.get (I) .delete ("3")) {list.remove (I) } / / deleted sequentially, but subscript and index are processed public static void remove3 (List list) {for (int I = 0, len = list.size (); I

< len; i++) { if (list.get(i).contains("3")) { list.remove(i); len--; i--; } } } // 在遍历过程中不直接操作原list public static void remove4(List list) { List temp = new ArrayList(); for (String str : list) { if (str.contains("3")) { temp.add(str); } } list.removeAll(temp); } public static void printList(List list) { for (String str : list) { System.out.println(str); } }} 如果还有其他好的方法再补充。 删除list中某个特定元素 当数据库查询出数据过后,发现有些数据不需要,所以这里要处理一下,加入循环list,根据判断条件去删除的话,就会出现异常。 所以这里用到了java8的新特性去删除。 List list = new ArrayList(); for (int i = 0; i < 50; i++){ UserFictitious userFictitious = new UserFictitious(); userFictitious.setUserFictitiousName(String.valueOf(i)); list.add(userFictitious); } list.removeIf(item ->

Item.getUserFictitiousName () .equals ("1"); System.out.println (list)

The underlying source code is deleted according to the characteristics of the iterator, and java8 is just packaged into removeIf, simplifying the code.

Default boolean removeIf (Predicate

Tags: Data conditions articles methods subscripts frequent meetings source code features processing code value reverse order elements interests beginners just at the same time underlying database more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Xiaomi MySQL NVidia