How to fill out an ArrayList with for each loop? (Java)

Ronny McNamara

I have to fill in an ArrayList with the first 10 multiples of two using a for each loop. I really can't seem to figure out how and I can't use any other loops.

Here is my code which is not working.

   ArrayList<Integer> arraylist = new ArrayList<Integer>(10);  
   for (Integer y : arraylist) {
         arraylist.add( (2+(y*2)));      
   }
Makoto

I'll address the point that everyone here is mostly glossing over: there is a key difference between a normal for statement and the enhanced-for statement.

The enhanced-for is defined in the JLS as syntactic sugar. What kind of sugar you get depends on what you're iterating over.

  • If you're iterating over an Iterable like an ArrayList, it is using the Iterator from that entity.

    for(I #i=Expression.iterator(); #i.hasNext();){
        VariableModifiersopt TargetType Identifier=
        (TargetType) #i.next();
        Statement
    }
    
  • If you're iterating over an array, then it's shorthand for the common for statements one is accustomed to doing.

    T[] #a = Expression;
    L1: L2: ... Lm:
    for (int #i = 0; #i < #a.length; #i++) {
        VariableModifiersopt TargetType Identifier = #a[#i];
        Statement
    }
    

Thus, the enhanced-for has one assumption:

What you're iterating over must be a non-empty collection or array.

The statement is only meant to iterate over existing elements, not provide a way or interface to iteratively add new elements.

There are any number of ways to sort this out, but the simplest is the most common approach: use a for statement.

ArrayList<Integer> arraylist = new ArrayList<Integer>(10);  
for (int i = 0; i < 10; i++) {
    arraylist.add( (2+(i*2)));      
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Do first 10 iterations of for each loop - java

来自分类Dev

Java For each loop 每个实例来自类

来自分类Dev

如何在Java中使用int / double in和out of loop

来自分类Dev

ArrayList的Java ArrayList

来自分类Dev

java arraylist复制的arraylist

来自分类Dev

ArrayList的ArrayList的Java大小

来自分类Dev

How to fill a Map<String, List<String>> from a text file ? -Difficulites dynamically naming each List

来自分类Dev

How to loop text file each line and append other text wrap each line

来自分类Dev

Java ArrayList的ArrayList非动态

来自分类Dev

Java中ArrayList中的ArrayList

来自分类Dev

Java Flood Fill问题

来自分类Dev

Java Fill嵌套HashMap

来自分类Dev

How can I make a field in a meteor Simple Schema equal to a js variable and still let the user fill out the rest of the simple schema?

来自分类Dev

Javascript for each loop

来自分类Dev

Java ArrayList存储

来自分类Dev

查找ArrayList Java的模式

来自分类Dev

Java ArrayList避免IndexOutOfBoundsException

来自分类Dev

Java ArrayList / RMI

来自分类Dev

java对象arraylist索引

来自分类Dev

java PreparedStatement Arraylist SQL

来自分类Dev

ArrayList模拟队列-Java

来自分类Dev

Java比较无序的ArrayList

来自分类Dev

TreeMap到ArrayList Java

来自分类Dev

Java ArrayList长度错误

来自分类Dev

Java堆栈与ArrayList

来自分类Dev

简单的ArrayList程序-JAVA

来自分类Dev

Java ArrayList覆盖

来自分类Dev

ArrayList上的Java递归

来自分类Dev

在ArrayList Java上重复