• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to update an Object within a Child Arraylist!  RSS feed

 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a parent Arraylist (ara) which contains a list of child ArrayLists (ar)!

I'm wondering what the syntax is that will allow me to update one of the child objects (Token class) within these Arraylists!

I have labeled some of the areas where the lists are being built with "<<<<<<<" and the very last one labeled "<<<<<<problem"
is where i'm trying to update a child object within these ArrayLists!

I would think it should look something like this: ara[i].set(j,tarj), but now realize that i would have to use an regular array of ArrayLists for this syntax, but i'm assuming there is a syntax for this particular scenario?

Thanks very much!

DATA sample:
06/25/02 |BUY| |ABGX |ABGX |500.00 | 4694.95|ABGENIX INC
06/03/03 |SEL| |ABGX |ABGX |500.00 | 4474.84|ABGENIX INC
06/08/95 |BUY| |ABX |ABX |875.00 | 22351.98|BARRICK GOLD CORP
06/14/95 |BUY| |ABX |ABX |1800.00 | 46777.47|BARRICK GOLD CORP
07/12/95 |BUY| |ABX |ABX |1000.00 | 27143.46|BARRICK GOLD CORP
06/06/96 |SEL| |ABX |ABX |3675.00 |106785.35|BARRICK GOLD CORP
07/31/96 |BUY| |ABX |ABX |2000.00 | 53964.13|BARRICK GOLD CORP
12/13/96 |SEL| |ABX |ABX |2000.00 | 57779.26|BARRICK GOLD CORP
12/31/01 |BUY| |AEGN |AEGN |750.00 | 2602.45|AEROGEN INC
05/16/01 |BUY| |AKSY |AKSY |250.00 | 2123.70|AKSYS LTD
12/17/01 |BUY| |AKSY |AKSY |250.00 | 1239.95|AKSYS LTD
12/31/01 |BUY| |AKSY |AKSY |250.00 | 1152.45|AKSYS LTD
04/20/01 |BUY| |ALT |ALT |250.00 | 934.95|ALTEON INC
12/05/01 |BUY| |ALT |ALT |500.00 | 1929.95|ALTEON INC
12/10/01 |BUY| |ALT |ALT |250.00 | 1029.95|ALTEON INC
03/13/03 |BUY| |ALT |ALT |500.00 | 2069.95|ALTEON INC
03/17/03 |SEL| |ALT |ALT |500.00 | 1979.99|ALTEON INC
03/18/03 |BUY| |ALT |ALT |500.00 | 1862.95|ALTEON INC
03/18/03 |SEL| |ALT |ALT |500.00 | 1834.99|ALTEON INC


 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is something like:

((ArrayList) ara.get(i)).set(j,tarj)
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Dave!

Thanks alot!

Now i'm wondering if it's possible to only update 1 field in that Object?

Instead of updating the whole Object back to the ArrayList, can i update just 1 field in that Object possibly?

ie ((ArrayList) ara.get(i)).set(j,tarj=>nshr) ?

where i would only update the nshr field?

Thanks again Dave!
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it Dave! very cool!

((ArrayList) ara.get(i)).set(j,tarj.nshr)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bob connolly:
Got it Dave! very cool!

((ArrayList) ara.get(i)).set(j,tarj.nshr)


Note that that will only put the nshr field from the object referenced by tarj into the ArrayList. The rest of the Token will completely disappear. If you need to keep the Token object intact, but just replace the nshr field, then you should probably do something like this:

Notice that I've split up your single line of code above into several lines. Personally, I think this helps with readability. Also, if you train yourself to think along these lines, I think you will be able to figure this out on your own when similar situations arise in the future.

I have one last comment on your choice of variable names. What does "nshr" mean? Personally, I dislike cryptic abbreviations like this. It would probably help you be able to read your own code if you use more descriptive names for your variables. Will you be able to read this code if you have to come back to it in 2 weeks? How about in 2 months? Or a year?

Layne
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Layne!

Good one!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!