• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Evildoers! Eat my justice! And this tiny ad's justice too!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic