Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

confusion about ArrayList

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this gives the result -
3
true
2

how is VM choosing which one to delete (among "hi" and s in line no 10 and 12 respectively) ? because when i am commenting out test.add("hi"); i am seeing that it is s that gets removed. But when they are both active,it is "hi" that is removed. What is the rule or logic behind this?

Since i am learning collections for the first time, it worries me a lot if the theory included in Sierra-Bates is enough.They gave very good introductions on comparable,comparator,equals,hashcode etc.But theories about List,Map, Set or its implementing classes are a bit...well a lot obscured . Do i need in-depth knowledge in those sections too for the sake of the exam?
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection List have three types of remove method.

  • remove(Object o)


  • Removes the first occurrence in this list of the specified element

  • remove(int index)


  • Removes the element at the specified position in this list

  • removeAll(Collection c)


  • Removes from this list all the elements that are contained in the specified collection

    In your case you are simply use remove(Object o) so that will remove first element find at your list.

    So in your case it will remove line ..

     
    Tanmoy Dhara
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    In your case you are simply use remove(Object o) so that will remove first element find at your list.




    now i have reversed the order of line 10 and line 12 ( putting test.add(s); before test.add("hi"); ).

    But the answer is still same
    3
    true
    2
    ( it was supposed to be 3false2 according to your argument, isn't it?)

     
    Sheriff
    Posts: 9697
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    This will assign a string object containing value hi from the String pool. When you write

    That same object from the pool is added to the list. So actually if you write

    or

    or

    they are all the same as both s or "hi" refer to the same String object in the string pool...
     
    Tanmoy Dhara
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    they are all the same as both s or "hi" refer to the same String object in the string pool...



    so that is why it is giving 'true'...right,i should have thought of that (exactly 13 days away from exam,but still.. )

    thanks to you both
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ankit Garg wrote:
    they are all the same as both s or "hi" refer to the same String object in the string pool...



    ...and even if they are *different* objects, but both strings are "hi", it will still work.

     
    Ranch Hand
    Posts: 814
    Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @ Matt

    Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).


    This is statement given in Java SE 6.0 API documentation for class ArrayList containing method remove(Object o). Even if both strings are different it will work.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi ,
    don't get confusion about Arraylist..


    ArrayList:

    1)The Underlying datastructure for ArrayList is
    Resizeable array or Growable Array.
    2) Insertion order is Preserved (i.e., order of elements inserted into arraylist).
    3)Duplicatate Objects are allowed.
    4)Hetrogenius Objects are Allowed.
    5)null insertion is possible.
     
    Ankit Garg
    Sheriff
    Posts: 9697
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Adan -eve please check your private messages for some administrative matter.
     
    Tanmoy Dhara
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ninad and Matt, i understand the code matt has written is executing because of the implementation of equals method in String class.
    Thanks to Adam Eve as well for help

     
    Bring me the box labeled "thinking cap" ... and then read this tiny ad:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic