@Data @Accessors(chain = true) public class Species { public String name; public Integer age; public Boolean sex; } @Data public class Person extends Species { private String address; private String phoneNumber; @Override public String toString() { return String.format("address:%s,phoneNumber:%s,name:%s,age:%s,sex:%s", address, phoneNumber, name, age, sex); } } public class Test { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Person person = new Person(); person.setName("tom" + i); person.setAge(20 + i); //person.setPhoneNumber("1352343212"+i); //person.setAddress(String.format("杭州%s区", i)); person.setSex(i % 2 == 0); personList.add(person); } personList.forEach(o -> { System.out.println(String.format("%s-%s", o.toString(), personList.indexOf(o))); }); } }
各位大神,截图中的 o.equals(elementData[i]) 为啥会相等,按理比较两个对象的内容 o对象的name,age和elementData[i]都不一样;求解~