[英]How to find a specific value inside an ArrayList and edit it
我有一个Item
类的ArrayList
,该Item
类包含5个String
变量(假设id , name , surname , email , city ),在初始化ArrayList
我要解析它。 我想要的是:
我有一个String x = "123"
,我想找到包含此id
的项目,然后可以对其进行编辑,我该怎么做?
我发现有一个方法MyList.contains(ItemExample)
但是使用此方法,我必须传递一个包含每个变量而不仅仅是ID的Item。
编辑:
for (int i = 0; i < MyList.size(); i++) {
System.out.println(MyList.get(i)); //for example I want to print ONLY the id, not the other variables inside MyList
}
您可以使用2种方法来做自己想做的事情:-
如果您的id
对于所有Item
对象都是唯一的,则可以使用Map<String, Item>
,其中key
是您的id
, value
是您的Item
对象。
Map<String, Item> map = new HashMap <String, Item>(); map.put(ID, new Item()); String idToSearch = ID_SEARCHED; Item searchedItem = map.get(ID_SEARCHED); if(searchedItem != null) searchedItem .setId(NEW_ID); else System.out.println("Item not found);"
如果id
不是唯一的,则您只需要使用List
并需要遍历整个list
,以便首先获取该对象,然后再设置新值。
List<Item> items = new ArrayList<Item>(); items.add(new Item()); String idToSearch = ID_SEARCHED; for(Item singleItem : items){ if(singleItem.getId().equals(ID_SEARCHED;)){ singleItem.setId(NEW_ID); } }
PS:-以ID_SEARCHED
方式,所有Item
对象的id
将被更新,其id
为ID_SEARCHED
您可以遍历列表。
如果按ID检索是一项频繁的操作,或者将其替换为Map,则更好。 映射是一对键值对的集合,您可以使用带有方法get(key)的键来访问值。
因此,您的代码变为:
Map<String, Item> map = ....
String idTosearch = ....
Item item = map.get(idTosearch);
请根据您的情况参考以下示例
import java.util.*;
public class TestStudent{
// find student by ID and modify it
public void findStudentByID(ArrayList al, int id){
Iterator itr=al.iterator();
while(itr.hasNext()){
Student st=(Student)itr.next();
if(st.rollno==id){
st.age=20;
st.name="Singh";
}
}
}
public void showStudents(ArrayList al){
Iterator itr=al.iterator();
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
public static void main(String args[]){
//Creating user-defined class objects
Student s1=new Student(101,"Girdhar",23);
Student s2=new Student(102,"Rathore",21);
Student s3=new Student(103,"Banna",25);
ArrayList<Student> al=new ArrayList<Student>();//creating arraylist
al.add(s1);//adding Student class object
al.add(s2);
al.add(s3);
TestStudent obj=new TestStudent();
//show before modifiying
obj.showStudents(al);
//update student
obj.findStudentByID(al, 102);
//show after modifiying
obj.showStudents(al);
}
}
class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
您可以尝试按以下方式进行映射:
import java.util.HashMap;
public class SordidSort {
public static void main(String args[]) {
//Use the map
HashMap<String, Item> itemMap = new HashMap<String, Item>();
//Create & Add the Item Objects to map with key as id value in Object
// Like
Item item = new Item();
item.id = "123";
itemMap.put(item.id, item);
//update id value
String x = "123";
if(itemMap.containsKey(x)){
Item serachedItem = itemMap.get(x);
if(serachedItem != null){
serachedItem.id = "321"; // new Id
itemMap.put(serachedItem.id, serachedItem); // Put Item Back to Map
itemMap.remove(x); // Remove old Item from Map , this is required if id is changed. not requred if other values changed.
}
}
}
}
class Item{
//Your attributes
String id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.