• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question regarding simple Java code.  RSS feed

 
Henrik Swensen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm very new to Java so be ready for some very basic questions.

I'm reading Head First Java by Kathy Sierra & Bert Bates and am "stuck" on an exercise on page 161.

The code is as following


--------------------

Now what I don't get is where does a.add("2.2"); go? When running the code I cannot see it although I perceive that it should get printed out in the console.
Shouldn't the 2.2 get printed out as another object in the ArrayList?


Thanks in advance!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at line 19.
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart is right, but just to elaborate

line 19: a.remove(2);

This removes the value at index 2, which happens to be "two".

This means that the if-statement condition in



is false because "a" does not contain "two" anymore, thus "2.2" will never be added to the list.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!