• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change an Element Value in an ArrayList of ArrayList  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two dimensional array created by <ArrayList<ArrayList<String>>

Contains item# and quantity

I need to change the quantity of a particular element with known item#.

I can loop through the array and get the element index. But I am not sure how to get a reference to value of column 2 so I can change the value.

I know it will be al.set(counter, my value)

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve, I don't think its 2-dimensional.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Hi Steve, I don't think its 2-dimensional.


I do not understand??

In the array at Index# 0 get(0) has item# and get(1) has quantity. It is the get(1) that I need to change.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the data modeled this way? Using arrays to represent "fields" rather than a class reminds me of 1970's FORTRAN.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why is the data modeled this way? Using arrays to represent "fields" rather than a class reminds me of 1970's FORTRAN.


I am not familiar with using a class to mimic an array. But I am willing to learn.

Regardless, despite my unorthodox approach to this is there a way to do what I am desiring?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
Bear Bibeault wrote:Why is the data modeled this way? Using arrays to represent "fields" rather than a class reminds me of 1970's FORTRAN.


I am not familiar with using a class to mimic an array.

It's not a class mimicking an array. Rather, you seem to be trying to use an array to mimic a class. You say:
In the array at Index# 0 get(0) has item# and get(1) has quantity.

So you have an entity that possesses two properties: an item number and a quantity. Why use an array for this rather than a class?


Using an array for this in an object-oriented language makes no sense at all.

Regardless, despite my unorthodox approach to this is there a way to do what I am desiring?

Not going to go there; it's just flat out the wrong approach.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:Contains item# and quantity

What, the String? Because nothing else does.

I need to change the quantity of a particular element with known item#.

OK, well this sounds like you're using Item# as in index to the element you want to change (or List...I'm still not quite sure).

I can loop through the array and get the element index.
You can? How?
But I am not sure how to get a reference to value of column 2 so I can change the value.
OK, so that sounds like each of your Lists contains two Strings, the second of which contains the quantity you want to change - is that right?
What does the first one contain?

I'm not trying to be obtuse, but do you see how complicated all that is - and also how much you haven't told us?

On the other hand if you had an Item class that included a number, you could make it the key to a Map that includes a quantity (which should NOT be a String) as it's value.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not start by creating an Item class. If you are going to use it as the “K” in a Map, the class ought to be immutable. If you on have Strings and primitives as fields, that should be quite easy to achieve.

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