Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having some trouble with this code.

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I've been reading the Java Head First 2nd Edition book lately, and I've come across this code that I'm having a hard time to read.



I'm just began learning ArrayLists and I'm having trouble understanding how the printAL method works. One last question that I have is how do the "add" methods work?
For example:

a.add(0, "zero");

Does this code mean that I'm adding an object to the ArrayList with a string value of "zero"?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printAl (shouldn't that be printAll) method uses an enhanced for-loop to print all the elements in the ArrayList. About your question about the add method. Just look at the documentation.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (String element : a1)

...means...

for each String (which we'll call "element") in a1.

So this will iterate through all Strings in a1. On the first iteration, "element" will reference the first String; on the second iteration, "element" will reference the second String; etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic