Results 1 to 8 of 8
  1. #1
    kishan's Avatar
    kishan is offline Member
    Join Date
    Apr 2009
    Posts
    57
    Rep Power
    0

    Post Regarding arrayList

    Hi,
    Please observer the code.


    ArrayList arrResultingActionoptions=buttonsForm.getDrpResult ingActionOptions();
    arrResultingActionoptions.remove(0);
    for(int i=0;i<arrResultingActionoptions.size();i++)
    {
    LabelValueBean labelValueBean=(LabelValueBean)arrResultingActiono ptions.get(i);
    if(labelValueBean.getValue().equalsIgnoreCase(strR esultingActionId))
    {
    arrResultingActionoptions.remove(i);
    break;
    }
    }
    buttonsForm.getDrpResultingActionOptions();


    Arraylist arrResultingActionoptions get from ButtonsForm classs.I removed some of the Ids of the arraylist according to my requirement. After executing the above code I observed "buttonsForm.getDrpResultingActionOptions()" at last.
    The elements which were removed from arraylist "arrResultingActionoptions" also removed from "buttonsForm.getDrpResultingActionOptions();". Why it happens like that eventhough I didnt set "arrResultingActionoptions" to "DrpResultingActionOptions".

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Most likely, buttonsForm.getDrpResult ingActionOptions() returns a reference to its internal ArrayList, rather a reference to a clone. The reason it does that is so that you can remove whatever you added to it.

    By the way, you remove the first element, and then you loop through the rest and remove them as well. Most likely, the list only had the one element in the first place. That's a good thing, because your loop will skip over every other element as you remove them. If the list had five elements, you would remove 0, which would cause element 1 to become element 0. Then you remove the new 1, which causes 2 to become 1. The best way to remove elements from a list is to obtain an Iterator and remove the elements using the Iterator. It handles all sorts of issues that will occur otherwise.

  3. #3
    kishan's Avatar
    kishan is offline Member
    Join Date
    Apr 2009
    Posts
    57
    Rep Power
    0

    Default

    Thanks for ur reply but my doubt is

    I removing the element from local arraylist of "arrResultingActionoptions" only .I dont do any modifications to arraylist "buttonsForm.getDrpResult ingActionOptions()".
    what happening actually is the modifications , which are done to the local arraylist "arrResultingActionoptions" are reflecting to the "buttonsForm.getDrpResult ingActionOptions()" also...
    why it happens like that?
    I want "buttonsForm.getDrpResult ingActionOptions()" remains unchanged.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Your problem is this: When you request the list of actions, you are getting a reference to the original list, not a copy of the list. This design is done on purpose, so you can modify the original list.

    If you want to modify a *copy* of the list, make a copy:

    ArrayList copyList = new ArrayList();
    copyList.addAll(originalList);

  5. #5
    kishan's Avatar
    kishan is offline Member
    Join Date
    Apr 2009
    Posts
    57
    Rep Power
    0

    Default

    thank u steve11235...........

    as u told we have to make copy of that arraylist and make modifications to the local arraylist.But in case of String or integer we directly assign those values to loacal varibles and do our modifications . These modifications do not reflect the original values.
    why? and do the reference cocept not apply here?

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    This has to do with Java internals. When you pass parameters, you *always* pass a copy of the field. However, programmers get confused about what is in a field.

    Primitive types are int, long, double, char, byte, etc. They are stored on the "stack". When you pass a primitive type as a parameter, a copy is made on the stack.

    All objects are stored in the "heap". You cannot store an object in a field. Instead, you store a "reference" to the object in a field, and a copy of the reference is made on the stack. In many ways, references act like primitive types.

    String is an object, but it has special properties, in that its value cannot be changed. A String is said to be "immutable". Any time you do an operation on a String, a *new* String is created on the heap, and a new reference is returned. Most objects are *not* immutable.

    As far as your ArrayList, there is one ArrayList instance on the heap, and a reference to it on the stack. When the reference is passed as a parameter or a return value, a copy is made, so there are now two references pointing to the one ArrayList. These references are called "aliases" because the one object is "known" by multiple references.

    I hope this helps. A good SCJP (Sun Certified Java Professional) preparation book covers everything you need to know about Java in an orderly fashion. If you are new to Java, it would be very slow going, but if you skip over the deeper concepts, you will learn all the basics, and then you can return to the parts you skipped over later.

  7. #7
    kishan's Avatar
    kishan is offline Member
    Join Date
    Apr 2009
    Posts
    57
    Rep Power
    0

    Default

    Thank u steve......

  8. #8
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Hi,
    You can use clone() method of ArrayList to solve this issue.

Similar Threads

  1. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 02:59 AM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 01:47 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •