Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java delete eligible data from list

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report