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

后端 未结 8 717
故里飘歌
故里飘歌 2020-12-19 08:26

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

Answer answer1;
Answer answer2;
Answer answer3;

ArrayList answers = new Array         


        
8条回答
  •  温柔的废话
    2020-12-19 08:47

    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.

提交回复
热议问题