操作list实体类——1、修改list里对象的值,无需返回,list里包含的即为修改后对象

修改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;  // 地区


}
(个人记录,希望能帮助新手!)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值