关于java的一些小问题,list中添加对象,java基本数据类型对象

List<QuestionAnswer> questionAnswers = new ArrayList<QuestionAnswer>();
List<Question> questions = survey.getQuestions();
Iterator<Question> it = questions.iterator();
Question question = new Question();
while(it.hasNext()){
question = it.next();

QuestionAnswer questionAnswer = new QuestionAnswer();

//必须每次new一个新的对象,否则add后一次添加会覆盖前一次。因为对于List<T>来说,如果T是引用类型,那保存的是引用,如果是值类型,保存的是值本身!

    BeanUtils.copyProperties(question, questionAnswer);
    questionAnswers.add(questionAnswer);

}


class  Test
{
public static void main(String[] args) 
{


Long a = 0L;
System.out.println("a == 0L ? " + (a==0L));
System.out.println("a == 0 ? " + (a==0));
Long b = 0L;
System.out.println("a == b ? " + (a==b));
System.out.println("a equals b ? " + (a.equals(b)));

Long c = new Long(0);
        Long d = new Long(0);
System.out.println("c == d ? " + (a==b));
System.out.println("c equals d ? " + (a.equals(b)));


Long e = new Long(3);
        Long f = new Long(3);
        System.out.println("e == f ? " + (e==f));


}
}




猜你喜欢

转载自blog.csdn.net/sinat_34126677/article/details/51625063