Please observer the code.
ArrayList arrResultingActionoptions=buttonsForm.getDrpResult ingActionOptions();
LabelValueBean labelValueBean=(LabelValueBean)arrResultingActiono ptions.get(i);
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".
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.
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.
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();
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?
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.
You can use clone() method of ArrayList to solve this issue.