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

The process by which Java uses list to store, traverse, and query specified information.

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the process of "Java using list to store, traverse, and query specified information". In daily operation, I believe many people have doubts about the process of storing, traversing and querying specified information in Java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "Java using list to store, traverse, query specified information". Next, please follow the editor to study!

Code content

Use list to save, traverse, query, delete

Import java.util.ArrayList;import java.util.List;/** * @ auther::9527 * @ Description: question 7 * @ program: multithreading * @ create: 2019-08-09 23:39 * / public class Seventh {public static void main (String [] args) {/ / initialize penguin information Pengui p1 = new Pengui ("Euro", "Q"); Pengui p2 = new Pengui ("Yaya", "Q sister") Pengui p3 = new Pengui ("Feifei", "Q sister"); Pengui p4 = new Pengui ("Meimei", "Q sister"); / / create List collection and add penguin information to list List list = new ArrayList (); list.add (p1); list.add (p2); list.add (p3); list.add (p4) / / View the penguin population System.out.println ("Total" + list.size () + "only penguins"); / / View the information of all penguins for (Pengui pengui: list) {System.out.println (pengui.getName () + "\ t" + pengui.getGender ()) } / / only Europe and Yaya are left in the specified penguin title, so what you want to delete is Feifei and Meimei for (int I = 0; I < list.size (); iTunes +) {if (list.get (I). GetName (). Equals ("Feifei")) {list.remove (I) } if (list.get (I). GetName (). Equals ("Meimei") {list.remove (I);}} System.out.println ("\ n\ n"); System.out.println ("only after deletion" + list.size () + "only penguins\ n" + "respectively:") / / check the remaining penguin for (Pengui pengui: list) {System.out.println (pengui.getName () + "\ t" + pengui.getGender ());} / / determine whether the penguin contains the specified penguin. String index = "Meimei" is specified in the title; / / set the temp temp value of the transfer query to a number int temp =-1 that will never appear in an array subscript. / / iterate through the collection, assign a value to temp if there is this element in the collection, and if it does not have this element in the collection, it will not be assigned for (int I = 0; I < list.size (); iSuppli +) {if (list.get (I). GetName (). Equals (index)) {temp = I }} / / if the value of temp does not change, there is no such element. If the value of temp changes, there is this element if (temp = =-1) {System.out.println ("the collection does not contain" + index+ "information");} else {System.out.println ("the collection contains" + index+ "information"). At this point, the study on "the process of storing, traversing, and querying specified information in Java with list" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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