如何使用java中的每个从数组创建一个Object [duplicate]

时间:2023-02-09 23:44:05

This question already has an answer here:

这个问题在这里已有答案:

Hi i am trying to do this and i cant understand why I am trying to create Pointe instance of an array from for each but it is coming out as null

嗨,我想这样做,我不明白为什么我试图从每个创建一个数组的Pointe实例,但它出来为null

Point arr[]=new Point[5]
for(Point o:arr){
o=new Point();
}

1 个解决方案

#1


1  

You inicialize the termporary variable o in the loop, not the items in the outer array:

你将循环中的临时变量o,而不是外部数组中的项目:

Point arr[]=new Point[5];
for(int i=0; i<arr.length; i++){
    arr[i] = new Point();
}

#1


1  

You inicialize the termporary variable o in the loop, not the items in the outer array:

你将循环中的临时变量o,而不是外部数组中的项目:

Point arr[]=new Point[5];
for(int i=0; i<arr.length; i++){
    arr[i] = new Point();
}