修改list里对象的值,无需返回,list里包含的即为修改后对象
public static void main(String[] args) {
Person p1 = new Person("d1","赵1", 10000, 18, "男", "北京");
Person p2 = new Person("d2","钱1", 12000, 20, "男", "上海");
Person p3 = new Person("d3","孙1", 14000, 22, "女", "广东");
Person p4 = new Person("d4","李1", 16000, 22, "女", "深圳");
List<Person> peopleList = new ArrayList<>();
peopleList.add(p1);
peopleList.add(p2);
peopleList.add(p3);
peopleList.add(p4);
editList(peopleList);
//extractList(peopleList);
//getObjByValue(peopleList,"d1","d3");
System.out.println(Arrays.toString(peopleList.toArray()));
}
示例:修改所有人姓名为 赵谦孙俪 ,年龄为 24
public static void editList(List<Person> peopleList){
for (Person person : peopleList) {
person.setName("赵谦孙俪");
person.setAge(24);
}
}
可看到list所有实体类name属性和age属性已修改
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String id; // id
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String area; // 地区
}
(个人记录,希望能帮助新手!)