• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Collection addAll method Strange Behavior

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am preparing for SCJP 1.4. Sunday is my exam!

I came across following question on collection addAll method:

What is the result of executing the following fragment of code:



The answer to above question is d. Which indicates that addAll does not adds a value if that is already present in collection.

Is this correct understanding? Shouldn't this be behavior of only Set collection? What all collection addAll method behaves in similar way?

Now here is bigger problem in my head:

if I replace line //3 addAdd above with following two lines:

s2.add("a");// 1
s1.addAll(2, s2);// 2

then the output of program is:

[a, b, a, c]

now here addAll is adding same element !!

I am confused, please help!
TIA!
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u read the api for the subList method, it says:


Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.)


If the fromindex and toIndex are same, the list returned is empty.
So, the statement:

will return u an empty list
Got it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic