动态地将类对象添加到Java的ArrayList中

huangapple go评论73阅读模式
英文:

Dynamically add class objects to ArrayList in java

问题

我知道如何将对象添加到 `ArrayList`

    class LightBulb {
        int position;
        LightBulb(int position) {
            this.position = position;
        }
    }

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

            LightBulb ob1 = new LightBulb(1);
            LightBulb ob2 = new LightBulb(2);
            LightBulb ob3 = new LightBulb(3);
            LightBulb ob4 = new LightBulb(4);
            LightBulb ob5 = new LightBulb(5);

            ArrayList<LightBulb> list = new ArrayList<LightBulb>(); 
            list.add(ob1);
            list.add(ob2);
            list.add(ob3);
            list.add(ob4);
            list.add(ob5);
        }
    }

有没有办法使用循环在运行时添加对象就像下面这样

    int n = 100;
    for(int i=0; i<n; i++) {
        LightBulb ob1 = new LightBulb(i);       // 但是这里是错误的,因为我们正在向 ArrayList 中添加相同的对象
        list.add(ob1);
    }
英文:

I know how to add objects to an ArrayList.

class LightBulb {
    int position;
    LightBulb(int position) {
        this.position = position;
    }
}

public class MainBulb
{
	public static void main(String[] args) {
		
		LightBulb ob1 = new LightBulb(1);
		LightBulb ob2 = new LightBulb(2);
		LightBulb ob3 = new LightBulb(3);
		LightBulb ob4 = new LightBulb(4);
		LightBulb ob5 = new LightBulb(5);
		
		ArrayList&lt;LightBulb&gt; list = new ArrayList&lt;LightBulb&gt;(); 
		list.add(ob1);
		list.add(ob2);
		list.add(ob3);
		list.add(ob4);
		list.add(ob5);
	}
}

Is there a way to add objects at runtime using a loop, like below?

int n = 100;
for(int i=0;i&lt;n;i++) {
    LightBulb ob1 = new LightBulb(i);       // But here it is wrong as we are adding same object to the ArrayList
    list.add(ob1);
}

答案1

得分: 1

new操作符在Java中总是创建一个新的Java对象。你的示例确实在创建新的灯泡对象。它们不是同一个对象。

英文:

new operator in java always creates a new Java object. Your example is indeed creating new Light Bulbs. They are not the same object.

答案2

得分: 1

你每次都在添加不同的对象。只需打印并检查一次,你就会明白。根据我理解你的问题,我将解释一个可能的误解。

我认为你感到困惑,因为你每次都使用相同的变量名 ob1 添加对象。在Java中,当你使用 new 关键字时,它会创建一个新的实例,因此对于每次迭代,你都会得到一个新的对象。

考虑一下,如果在第一次迭代中有 lightbulb1

ob1=lightbulb1。

但在第二次迭代中,ob1 变量指向值为2的新对象,即

ob1=lightbulb2

这将在循环结束之前一直重复下去。

因此,在每次迭代结束时,你的列表都被填充了不同的对象。

英文:

You are adding different objects each time. Just print and check once you will get clarity. From what I understood from your question, I will explain a possible misconception.

I think you are getting confused cause you are adding object with same variable name ob1. In Java, when you use the new keyword, it will create a new instance, so for each iteration you are getting new object.

Consider if you have lightbulb1 in your first iteration.

ob1=lightbulb1.

but in second iteration ob1 variable points to new object with value of 2, i.e

ob1=lightbulb2

This will be repeated till the end if the loop.

So, your list is filled with different objects at the end of your each iteration.

答案3

得分: -2

你的代码实际上已经输出结果。我相信你对于将相同的对象名称 "ob1" 分配给每个新创建的对象感到困惑。如果直接将动态创建的类对象添加到 ArrayList 中而不创建对它的引用,这个问题就可以解决。
将 for 循环修改如下。

int n = 100;
for (int i = 0; i < n; i++) {
    list.add(new LightBulb(i + 1));
}

我使用了 i+1 进行初始化,因为在你的问题中,你使用的是从 1 到 5 的值进行初始化,而不是从 0 到 4 的值。

英文:

Your code is actually giving output. I believe you are having confusion about assigning same object name "ob1" to each newly created object. This can be solved, if you directly add the dynamically created class object into the ArrayList without creating a reference to it.
Change the for loop as follows.

int n = 100;
for(int i=0;i&lt;n;i++) {
    list.add(new LightBulb(i+1);        
}

I used i+1 to initialize, because in your question, you are initializing with value 1 to 5 not with values 0 to 4

huangapple
  • 本文由 发表于 2020年9月5日 11:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63750229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定