Java:使用 FOR 循环将元素添加到 arraylist,其中元素名称的数量增加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8268921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:28:41  来源:igfitidea点击:

Java: Add elements to arraylist with FOR loop where element name has increasing number

javafor-loop

提问by Rupal

I have an arraylist where I want to add elements via a for loop.

我有一个数组列表,我想在其中通过 for 循环添加元素。

Answer answer1;
Answer answer2;
Answer answer3;

ArrayList<Answer> answers = new ArrayList(3);

for (int i=0;i<3;i++)
{
    answers.add(//HOWTO: Add each of the answers?);
}

EDIT: How would this go if I have, let's say 50 Answer elements?

编辑:如果我有,比方说 50 个答案元素,这会怎样?

采纳答案by Chris

You can't do it the way you're trying to... can you perhaps do something like this:

你不能按照你想要的方式去做......你能不能做这样的事情:

List<Answer> answers = new ArrayList<Answer>();
for(int i=0; i < 4; i++){
  Answer temp = new Answer();
  //do whatever initialization you need here
  answers.add(temp);
}

回答by Thomas Uhrig

why you need a for-loop for this? the solution is very obvious:

为什么你需要一个 for 循环?解决方案非常明显:

answers.add(answer1);
answers.add(answer2);
answers.add(answer3);

that's it. no for-loop needed.

就是这样。不需要 for 循环。

回答by Jo?o Silva

That can't be done with a for-loop, unless you use the Reflection API. However, you can use Arrays.asListinstead to accomplish the same:

for除非您使用反射 API,否则无法使用-loop完成。但是,您可以使用Arrays.asList代替来完成相同的操作:

List<Answer> answers = Arrays.asList(answer1, answer2, answer3);

回答by helpermethod

Put the answers into an array and iterate over it:

将答案放入一个数组中并对其进行迭代:

List<Answer> answers = new ArrayList<Answer>(3);

for (Answer answer : new Answer[] {answer1, answer2, answer3}) {
    list.add(answer);
}

EDIT

编辑

See Jo?o's answer for a much better solution. I'm still leaving my answer here as another option.

请参阅 Jo?o 的答案以获得更好的解决方案。我仍然将我的答案留在这里作为另一种选择。

回答by NPE

If you simply need a list, you could use:

如果您只需要一个列表,您可以使用:

List<Answer> answers = Arrays.asList(answer1, answer2, answer3);

If you specifically require an ArrayList, you could use:

如果您特别需要ArrayList,则可以使用:

ArrayList<Answer> answers = new ArrayList(Arrays.asList(answer1, answer2, answer3));

回答by BuZz

There's always some reflection hacks that you can adapt. Here is some example, but using a collection would be the solution to your problem (the integers you stick on your variables name is a good hint telling us you should use a collection!).

总有一些你可以适应的反射技巧。这是一些示例,但使用集合将是您问题的解决方案(您粘贴在变量名称上的整数是一个很好的提示,告诉我们您应该使用集合!)。

public class TheClass {

    private int theField= 42;

    public static void main(String[] args) throws Exception {

        TheClass c= new TheClass();

        System.out.println(c.getClass().getDeclaredField("theField").get(c));
    }
}

回答by xorange

Thomas's solution is good enough for this matter.

对于这个问题,托马斯的解决方案已经足够好了。

If you want to use loop to access these three Answers, you first need to put there three into an array-like data structure ---- kind of like a principle. So loop is used for operating on an array-like data structure, not just simply to simplify typing task. And you cannot use FOR loop by simply just giving increasing-number-names to the elements.

如果要使用循环访问这三个答案,首先需要将其中三个放入一个类似数组的数据结构中----有点像一个原理。所以循环用于操作类似数组的数据结构,而不仅仅是为了简化打字任务。而且您不能通过简单地为元素提供递增数字名称来使用 FOR 循环。

回答by Shubham Chaurasia

Using Random function to generate number and iterating them on alusing for loop

使用 Random 函数生成数字并al使用 for 循环迭代它们

ArrayList<Integer> al=new ArrayList<Integer>(5);
    for (int i=0;i<=4;i++){

       Random rand=new Random();
        al.add(i,rand.nextInt(100));
        System.out.println(al);
    }
System.out.println(al.size());