ArrayList';s indexOf()在java中返回-1

ArrayList';s indexOf()在java中返回-1,java,list,comparison,return-value,indexof,Java,List,Comparison,Return Value,Indexof,我正在用数据库中的数据验证文件中的数据列表,以避免在数据库中重复插入,如果存在重复,则我希望该记录在文件中的索引,我使用以下代码 List<StudentMaster> studentMasterListFromDB = studentMasterDao.getStudentList(); List<StudentMasterVO> studentMasterListFromFile = getStudentMasterListFromFile(); for(int i

我正在用数据库中的数据验证文件中的数据列表,以避免在数据库中重复插入,如果存在重复,则我希望该记录在文件中的索引,我使用以下代码

List<StudentMaster> studentMasterListFromDB = studentMasterDao.getStudentList();
List<StudentMasterVO> studentMasterListFromFile = getStudentMasterListFromFile();

for(int index=0;index<studentMasterListFromDB.size();index++){
    StudentMasterVO studentMasVO = new StudentMasterVO();
    StudentMaster studentMaster = studentMasterListFromDB.get(index);       
    BeanUtils.copyProperties(studentMasVO, studentMaster);      
    int indexOfexistingRec = studentMasterListFromFile.indexOf(studentMasVO);
    System.out.println("indexOfexistingRec :"+indexOfexistingRec);


}
List studentMasterListFromDB=studentMasterDao.getStudentList();
List studentMasterListFromFile=getStudentMasterListFromFile();

对于(int index=0;index您需要在
StudentMaster
StudentMasterVO
类中实现
hashCode
equals
方法,以便正确比较它们是否相等

如果您使用的是Eclipse(或任何其他IDE),那么它可以为您生成这些方法,只需选择在比较时要考虑哪些字段


例如,如果您不熟悉这一概念,请参阅本文:

您需要在
StudentMaster
StudentMasterVO
类中实现
hashCode
equals
方法,以便正确比较它们是否相等

如果您使用的是Eclipse(或任何其他IDE),那么它可以为您生成这些方法,只需选择在比较时要考虑哪些字段


例如,如果您不熟悉这一概念,请参阅本文:

您需要在
StudentMaster
StudentMasterVO
类中实现
hashCode
equals
方法,以便正确比较它们是否相等

如果您使用的是Eclipse(或任何其他IDE),那么它可以为您生成这些方法,只需选择在比较时要考虑哪些字段


例如,如果您不熟悉这一概念,请参阅本文:

您需要在
StudentMaster
StudentMasterVO
类中实现
hashCode
equals
方法,以便正确比较它们是否相等

如果您使用的是Eclipse(或任何其他IDE),那么它可以为您生成这些方法,只需选择在比较时要考虑哪些字段




例如,如果您不熟悉这一概念,请参阅本文:

-1
表明未找到您要查找的项目。感谢您的回复。在我的列表中,搜索记录在那里,但显示为-1。抱歉,这是BeanUtils.copyProperties()方法。手动递增索引会导致崩溃。
-1
表示找不到您要查找的项目。感谢回复。在我的列表中,搜索记录在那里,但显示为-1。抱歉,这是BeanUtils.copyProperties()方法。手动递增索引会导致崩溃。
-1
表示找不到您要查找的项目。感谢回复。在我的列表中,搜索记录在那里,但显示为-1。抱歉,这是BeanUtils.copyProperties()方法。手动递增索引导致崩溃。
-1
表示找不到您要查找的项目。感谢回复。在我的列表中,搜索记录在那里,但显示为-1。抱歉,这是BeanUtils.copyProperties()方法。手动递增索引导致崩溃。感谢回复,我已经使用了equals()和hashcode()对于StudentMaster和StudentMasterVO。但我仍然有相同的问题。即使我也检查了哈希代码,哈希代码也正确。那么,copyProperties做什么呢?它是一种BeanUtils方法,只是将一个对象的值复制到另一个对象中,从StudentMaster对象复制到StudentMasterVO对象。但我认为这并不重要l问题。如果您的类正确地实现了hashcode和equals,并且如果列表中确实有与您正在搜索的相等的对象,那么它就没有理由不起作用。也许您调用copyProperties时使用了错误的参数顺序。感谢您的回复,我已经使用了equals()和hashcode()对于StudentMaster和StudentMasterVO。但我仍然有相同的问题。即使我也检查了哈希代码,哈希代码也正确。那么,copyProperties做什么呢?它是一种BeanUtils方法,只是将一个对象的值复制到另一个对象中,从StudentMaster对象复制到StudentMasterVO对象。但我认为这并不重要l问题。如果您的类正确地实现了hashcode和equals,并且如果列表中确实有与您正在搜索的相等的对象,那么它就没有理由不起作用。也许您调用copyProperties时使用了错误的参数顺序。感谢您的回复,我已经使用了equals()和hashcode()对于StudentMaster和StudentMasterVO。但我仍然有相同的问题。即使我也检查了哈希代码,哈希代码也正确。那么,copyProperties做什么呢?它是一种BeanUtils方法,只是将一个对象的值复制到另一个对象中,从StudentMaster对象复制到StudentMasterVO对象。但我认为这并不重要l问题。如果您的类正确地实现了hashcode和equals,并且如果列表中确实有与您正在搜索的相等的对象,那么它就没有理由不起作用。也许您调用copyProperties时使用了错误的参数顺序。感谢您的回复,我已经使用了equals()和hashcode()对于StudentMaster和StudentMasterVO。但我仍然有相同的问题。即使我也检查了哈希代码,哈希代码也正确。那么,copyProperties做什么呢?它是一种BeanUtils方法,只是将一个对象的值复制到另一个对象中,从StudentMaster对象复制到StudentMasterVO对象。但我认为这并不重要l问题所在。如果您的类正确地实现了hashcode和equals,并且如果列表中确实存在与您正在搜索的相等的对象,那么它就没有理由不起作用。也许您调用copyProperties时使用了错误的参数顺序。