• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList - Iterating and Loading  RSS feed

 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList fooList: int id, String name, String desc, ArrayList<BeanBar> subs

The ArrayList fooList already has values for id, name, desc for each element.

I want to iterate through the ArrayList fooList and for each element, use a function to fetch ArrayList barList and load subs with barList.

I can't quite understand the use of Iterator to do this.

Eclipse is saying subs cannot be resolved or is not a field.

I assume only one instance of .next() is required to advance the array cursor -- in the expression to be evaluated.

Maybe I'm not understanding what iterator is holding.

Any help?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterator#next returns the next element in the array. What is the type of the element? A custom class?

You probably don't need an Iterator, though. A for loop will work:
 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Iterator#next returns the next element in the array.

Not familiar with that syntax. I'm reading Java in a Nutshell, 6th Ed., Benjamin J. Evans & David Flanagan, pg246.
You probably don't need an Iterator, though. A for loop will work:

Can you elaborate?

I will try:
Eclipse does not complain, but somehow, for each of its elements, fooList has the same, last fetched, barList. Unfortunately,isn't showing me what I want to see. I get:where the hash is different for each element.
 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the toString() returns what is to be expected.

Now, how to get the entirety of the ArrayList stringified?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the declaration ArrayList<BeanBar> subs, then the iterator.next() method will return a BeanBar object as long as there is still one left in the collection that you're iterating over.

Think of it like one of those old gumball machines: you turn a knob and a gumball comes out. The balls of gum inside the machine are your individual objects, which in this case are BeanBar objects. These objects are contained in an ArrayList. Let's say the ArrayList is like a bag of gumballs. That's your Collection. When you get an Iterator for that Collection, that's like pouring that bag of gumballs into the machine. The machine has a knob, which is the next() method. Every time you call the next() method, it's like turning the knob: it returns another object (a BeanBar object) from the collection. You can keep turning that knob and expect another object/gumball until you run out of objects/gumballs.

So, the statement iterator.subs does not make sense because the iterator has no method or property called subs. It only has next() and other methods that either do something, like the optional remove() method, or inform you of the iterator's current state, like hasNext() to see if another call to next() would succeed.

 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iterator.next() does return the next element of the ArrayList, and from that I continue to specify the .id parameter as the argument to the function call.

The question is, how do I get the results from the function into that element of ArrayList where the cursor is supposedly pointing?

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

You are using terms that make what you're writing confusing and difficult to follow.

"use a function", "function call", and "results from the function" -- having to translate "function" to "method" creates cognitive dissonance and overhead

"foo" and "bar" are nonsense names and make it difficult for people to make an association between the two and to their apparent attributes.

"BeanBar" is not only an nonsense name, it reverses the order of usual naming convention which would make it "BarBean" instead, as in "PersonBean" or "WhateverBean"

In this code:

the loop variable, fooElement, is declared as an ArrayList. Again, an ArrayList has no subs property. If I'm reading what you initially wrote correctly, whatever kind of object the ArrayList fooList contains is what has the subs property. It appears you're still not properly distinguishing between an ArrayList and an Iterator, and the type of thing that these two contain or iterate over. Perhaps it would clear things up for us if you shared the declaration of this BeanBar object.



 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My latest code:
This is the console output, from println():
Note the first element's third field (it's second field is 'Health') has changed.

So, I am getting the results from the functionmethod into that element of HubList where the cursor is supposedly pointing.

The question is: why isn't it staying that way? Why is the third field of the last iteration bleeding into the the third field of the previous elements?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to guess that something is declared static which shouldn't be declared static.
 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like that answer! Unfortunately, unless something appears to behave as static but not be declared as static, then it's not a solution.

The only thing static is getting a connection to the database.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then I'm going to guess that there's a bug in the toString() method of whatever type your hubList variable is.

Emphasis is on the word "guess" -- without seeing any code that's all that can be done.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another guess would be that the DAO.query4SpokeGroup() does not instantiate a new list object each time it is called but returns an instance variable instead. Show us what that method does.
 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Returns an instance variable."

Interesting. So, if I declare a field (I guess instance variables are sometimes called fields) as a new ArrayList, it's not really a new ArrayList every time I use that class.

Ok, I did put a new ArrayList in the functionmethod that builds the ArrayList.

So far, I'm getting what I need to get.

Thank you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Smither wrote:Interesting. So, if I declare a field (I guess instance variables are sometimes called fields) as a new ArrayList, it's not really a new ArrayList every time I use that class.

Brian,

I suspect there are at least two problems going on here:
1. You're far too "close" to this code, so you're only thinking of the problem in terms of mechanics - ie, "how do I do it in Java".
2. You haven't explained the problem to US very well - probably, again, because you're far too close to this code.

So, my suggestions:
1. Stop Coding.
2. Instead of telling us about all the different Java "knobs" that you're twiddling, back up and tell us what you want to do.

And in order to do that, you need to give us some context:
What it is a "Hub"?
What does it look like? Real code please; not "mock-ups".
What is the 'spokesDAO'? DAO suggests a data access object, but that can mean almost anything. 'Spokes' suggests that "hub" might be a hub for a bicycle wheel, but again, we're shooting in the dark here.

You are familiar with this structure and what it's designed to do, we're not; so try and help us out as much as you can.

Also: as long as you're obsessed with bits and bytes and Java verbs, you're unlikely to solve your problem. Solutions come from thinking and understanding; not from Java code.

And if you have many things you want to do, take them one at a time.

HIH

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