直接上代码
public static void main(String[] args) {
List<Customer> addressList = new ArrayList<>();
Customer c1 = new Customer();
c1.setId("1");
c1.setName("ckf");
addressList.add(c1);
Customer c2 = new Customer();
c2.setId("2");
c2.setName("ycw");
addressList.add(c2);
Customer c3 = new Customer();
c3.setId("3");
c3.setName("ccc");
addressList.add(c3);
//方式一
for(int i=0; i< addressList.size(); i++){
String name = addressList.get(i).getName();
if ("ycw".equals(name)){
Customer customer = addressList.get(i);
//把当前循环的元素删除
addressList.remove(i);
//把name=ccc的数据添加到集合第0位
//addressList.add(0, customer);
//把name=ccc的数据添加到集合的最后一位
addressList.add(addressList.size(), customer);
}
}
//方式二
for (Customer customer : addressList) {
String name = customer.getName();
if ("ycw".equals(name)){
//把当前循环的元素删除
addressList.remove(customer);
//把name=ccc的数据添加到集合第0位
addressList.add(0, customer);
}
}
System.out.println(addressList);
}
不懂下方留言 谢谢