简体   繁体   中英

What does += do for list in Scala?

What does the += do here ? Does it mean append to the list? If maxNodes = 16, Do the below lines mean that values 0 to 15 are stored in NodeList? Or is it 0 to 16?

var Nodelist = new ArrayBuffer[Int]()
for (i <- 0 until maxNodes) {     
          Nodelist += i  
 }

You're just appending the Integer value in variable i to the list. Why not try this in the REPL?

According to the Javadoc , += in ArrayBuffer is an "alias" for addOne() . So at the beginning NodeList is empty (I'd rather call it nodeList ). On the first go at the For loop, the integer 0 is added to NodeList . On the next go around, the integer 1 will be added to NodeList , and so and so forth. Since you used until rather than to , the iteration will stop one short of maxNodes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM