Results 1 to 8 of 8
Thread: Regarding arrayList
- 07-25-2009, 09:02 AM #1
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".
- 07-26-2009, 06:41 AM #2
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.
- 07-27-2009, 07:50 AM #3
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.
- 07-28-2009, 07:37 AM #4
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();
- 07-28-2009, 08:07 AM #5
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?
- 07-28-2009, 08:23 AM #6
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.
- 07-29-2009, 07:54 AM #7
Thank u steve......
- 08-07-2009, 01:48 PM #8
- By gnarly hogie in forum New To JavaReplies: 2Last Post: 12-11-2008, 02:59 AM
- By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 12:43 PM
- By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 01:47 AM
- By kizilbas1 in forum New To JavaReplies: 11Last Post: 12-05-2007, 08:30 PM
- By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 07:45 PM