# For expert on complex loops (reposted) - complex looping problem

Discussion in 'Java' started by news.amnet.net.au, Apr 13, 2004.

1. ### news.amnet.net.auGuest

Hi

I am sorry to repost this, but I am still hoping someone with a great deal
of experience with complex loops may be able to help me with this rather
intractable problem.

(If someone can just help me with the looping problem without addressing the
referencing problem, that would be great also.)

Here goes again (repost):

I have a series of values I am getting from an Oracle table. Let's call some
of them "left values" (from the left column) and other "right values" (from
the right column). For every left value, the table finds one or more (most
likely more) right values. After this has been done, each right value
becomes the left value, for which then more right values are found.

The left values should go into the first dimension of my 2D array, the right
values in the second dimension.

So if I do a query for left value A and find right values B,C and D then my
array will contain:

myArray[A]
myArray[A][C]
myArray[A][D]

However, in the second query to my table, B becomes the "left value" so that
then I will need to put B in my the first dimension of my array. Suppose B
finds the following right values: E and F, I have to place the following
values into my array:

myArray[E]
myArray[F]

Then on for C -> finds right values G, H and I:

myArray[C][G]
myArray[C][H]
myArray[C]

and so on...

I am not sure what kind of complex loop I have to create to make this
happen. Any help with this will be greatly appreciated. The other problem is
that I do not know in advance how many values there will be, so an ArrayList
may be better than an Array. However, I can find no information on two
dimensional ArrayLists - how are they initialized and declared?

However, there is an added complication: I need to preserve the left ->
right relationships for future use so that I can display the array values in
a web page in such a way that it is clear that A has rightward relationships
to B, C and D, B has rightward relationships to E and F etc.

So if the relationships are indicated by identical numbers I need something
like this:

myArray[A] 1
myArray[A][C] 1
myArray[A][D] 1

myArray[E] 2
myArray[F] 2

etc..

However the number should be individually associated with the left value AND
with the right values, so that if I display the value of myArray[A] once
only i.e.

A

then the values for the right relationships:

B
C
D

I should have a reference pointer in each dimension i.e.

A -> ref 1

then the values for the right relationships:

B -> ref 1
C -> ref 1
D -> ref 1

(otherwise I will have to always display the linked values together).

So I need some sort of a hash table to be incorporated into the 2
dimensional array.

A tall order, but perhaps there is someone out there who has experience with
this?

Any help will be greatly appreciated.

Thanks very much

Hugo

news.amnet.net.au, Apr 13, 2004

2. ### =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=Guest

On Tue, 13 Apr 2004 14:50:49 +0800, news.amnet.net.au wrote:

> Hi
>
> I am sorry to repost this, but I am still hoping someone with a great deal
> of experience with complex loops may be able to help me with this rather
> intractable problem.
>
> (If someone can just help me with the looping problem without addressing
> the referencing problem, that would be great also.)

I suggest the following data structures:

a Map whose "key" is the "left" value and whose value is a Collection of
the "right" values. If order is important, use a List, otherwise a Set.

a Queue of values left to process (possibly a List, Set, or Stack).

Pseudo code:

HashMap relationships = new HashMap();
Stack queue = new Stack();
while ( ! queue.isEmpty() )
{
String current = (String) queue.pop();
// run query to get right values
for each right
{
if (! relationships.containsKey(right) && ! queue.contains(right) )
{ // don't want to get into an infinite loop!
queue.push( right );
}
Set c = (Set) relationships.get( current );
if ( c == null )
{
relationships.put( current, c = new HashSet() );
}
}

}

HTH,
La'ie Techie

=?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=, Apr 13, 2004