Skip to main content

ArrayList objects



Need some inputs:





Lets say i have N ArrayList and in each i am adding foo() object.







Foo foo = new Foo()



A.add(foo);



B.add(foo);



N.add(foo);







Now modification done on any one foo() object will reflect in all the other arraylist?









  1. If YES WHY? and









  2. whether this behaviour can also be achieved using any other collection like Vector etc...?









  3. IF i make foo as null will it reflect in all arraylist?






Comments

  1. Yes, because all lists only contain a reference to the same objects
    Yes, all collections work like that
    No, because you can only set a reference to null, and each list has a copy of the reference.

    ReplyDelete
  2. Any implementation of Collection API such as ArrayList or Vector hold reference to an object in heap memory so when you would get an index of a List by get(index) method, you achive reference to object so:


    Yes, if get an index of list by get(index) method and then change the stat of the achieved object, changes stay in memory.
    Yes,All Collection API have this behavior.
    No,When achieve to a index of list, act is: "You achieve a copy of reference to object" and when set it to null, list instance don't any change.

    ReplyDelete
  3. Because they all share the same reference.
    Any collection that Foo will reside in (synchronized wrapper implementations however will not allow concurrent modification)
    EDIT: (thanks to Michael) No quote " foo = null has no effect whatsoever on the object itself (Objects cannot be null), it only removes the reference and if there is no other reference the object will eventually be picked up by the garbage collector"

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.