ArrayList of ArrayLists - how to get the values out?

N

news.amnet.net.au

Hi

I found that I need to store a collection of objects as ArrayLists inside
another ArrayList, because if I store my (objects in the) ArrayLists in a
Map type Collection, iterating over the map does not give me first key in ->
first key out.

It was suggested to me I use LinkedHashMap, but that only exists in Java
platform 1.4 and I am working with 1.3.1 as this is what is available within
my Oracle environment.

Hopefully an ArrayList of ArrayLists will give me FIFO.

Excuse my ignorance on this one as I have never used an ArrayList of
ArrayLists - my question is: how do I get the values out?

My "outer" ArrayList is called indexed_relationships and the inner ones are
all called "p", (both have been declared and initialised earlier on) I
tried:

I have tried several things, but none work. For example, I tried ( I know
this is wrong, but it is what I could think of):

for(int i = 0; i < indexed_relationships.size(); i++ ) {
for (int j = 0; j < p.size(); j++) {
System.out.println(indexed_relationships.get(i).get(j).toString() +
"<br/>");
}
}

Any help will be greatly appreciated.

Thanks very much

Hugo
 
R

Roedy Green

Excuse my ignorance on this one as I have never used an ArrayList of
ArrayLists - my question is: how do I get the values out?

You will get your head bitten off if you ask questions like that
BEFORE you at least look at the JavaDoc and google for a tutorial.

Further these are newbie questions, which belong in
comp.lang.java.help.

ArrayList is a quite straightforward class with few surprises other
than you can't add elements out past the end, just at the end.

see http://mindprod.com/jgloss/gettingstarted.html
 
B

Bjorn Abelli

...
for(int i = 0; i < indexed_relationships.size(); i++ )
{
for (int j = 0; j < p.size(); j++)
{
System.out.println(
indexed_relationships.get(i).get(j).toString()
+ "<br/>");
}
}

ArrayLists stores Objects.

In version 1.5 you can use the generics to make an ArrayList work with only
instances of a specific subclass to Object, but until then you need to cast
the objects to the actual type when you're getting them out of an ArrayList.

((ArrayList)
indexed_relationships.get(i)).get(j).toString()

However, there's another jump in your thoughts here, as there's no "p"
available. The ArrayList instances doesn't have any "names" when they are
inside another ArrayList. Hence you cannot know the size of it "beforehand",
unless you have the same size on all contained ArrayLists, but then you
wouldn't even have to *check* the size.

for(int i = 0; i < indexed_relationships.size(); i++ )
{
ArrayList p =
(ArrayList)
indexed_relationships.get(i);

for (int j = 0; j < p.size(); j++)
{
System.out.println(
p.get(j).toString() + "<br/>");
}
}
Any help will be greatly appreciated.

You will get much more help if you post to the correct newsgroup. For
questions concerning newbie questions such as this you will be better off in
comp.lang.java.help.

// Bjorn A
 
J

John C. Bollinger

news.amnet.net.au said:
Hopefully an ArrayList of ArrayLists will give me FIFO.

Any List implementation preserves the order of elements modulo the
specific effects of List modifications -- that's part of what makes it a
List. If you add elements only at the end and process only from
beginning to end then you get FIFO behavior.
Excuse my ignorance on this one as I have never used an ArrayList of
ArrayLists - my question is: how do I get the values out?

You get the inner ArrayLists out of the outer ArrayList one at a time,
then for each one you get the values out, one at a time. This is not
much different from how you would do it if the outer collection were a
Map (or Set) rather than a List.
My "outer" ArrayList is called indexed_relationships and the inner ones are
all called "p", (both have been declared and initialised earlier on) I

No, it isn't and they aren't. Collections are one of the places in the
Java platform API where it is essential to understand the difference
between objects, references to objects, and reference-type variables.
Of the three, only the variables names. Object references can be stored
in Collection objects, in arrays, and in variables, and the same
reference (a value) can be stored in multiple places. The fact that a
reference to some object was once stored in a variable of some
particular name in some particular scope does not confer that name upon
the reference value or the object to which it refers.
tried:

I have tried several things, but none work. For example, I tried ( I know
this is wrong, but it is what I could think of):

for(int i = 0; i < indexed_relationships.size(); i++ ) {
for (int j = 0; j < p.size(); j++) {
-----------------------^^^^^^^^
This is wrong, because nothing associates variable "p" with the
particular ArrayList you want to iterate over. You need to retrieve the
desired inner List from the outer List before starting the iteration.
System.out.println(indexed_relationships.get(i).get(j).toString() +
-----------------------------------------------------^^^^^^
This is wrong because the return value of List.get() is Object; you have
to cast to ArrayList (or List) before invoking get(int) on the result.
Moreover, it is dreadfully inefficent to retrieve and cast the inner
List at each iteration of the inner loop, when presumably the outer List
hasn't changed.
"<br/>");
}
}

Also, although you can do an indexed iteration as above, I generally
recommend using an Iterator instead. At first it may seem less natural
if you're coming from, for instance, C, but it has several advantages
(that I won't go into at the moment) and few disadvantages.


John Bollinger
(e-mail address removed)
 
J

Jason Bell

Jeez put the guy of his misery :)

Iterator iterator = myMainArray.iterator();
while(iterator.hasNext()){
ArrayList tempArray = (ArrayList)iterator.next();
Iterator newiterator = tempArray.iterator();
while(newiterator.hasNext()){
// do what you need to do.
}
}

Is that the sort of thing you are after?

All very well thinking about generics in JDK1.5 but it ain't production so I
wouldn't touch it until it is so.

Regards
Jase Bell
 
M

mromarkhan

Peace be unto you.
<code>import java.util.Collections;
import java.util.ArrayList;
import java.util.Iterator;

public class Fifo
{
public static void main(String [] args)
{
ArrayList fruits = new ArrayList();

ArrayList mango = new ArrayList();
mango.add("Mango");
mango.add("Delicious");
mango.add("Yellow");
fruits.add(mango);
ArrayList apple = new ArrayList();
apple.add("Apple");
apple.add("Yummy");
apple.add("Red");
fruits.add(apple);
ArrayList tomato = new ArrayList();
tomato.add("Tomato");
tomato.add("Taste like vinegar");
tomato.add("Red");
fruits.add(tomato);

Collections.reverse(fruits);
int count = 1;
for(Iterator c = fruits.iterator(); c.hasNext();)
{
for(Iterator d = ((ArrayList)c.next()).iterator();
d.hasNext();)
{
System.out.println((String)d.next());
}
System.out.println("End details of fruit #" + count);
count++;
}
}
}
</code>
Outputs.
Tomato
Taste like vinegar
Red
End details of fruit #1
Apple
Yummy
Red
End details of fruit #2
Mango
Delicious
Yellow
End details of fruit #3

I am not sure about the requirements.
Have a good day.
 
R

Roedy Green

Any List implementation preserves the order of elements modulo the
specific effects of List modifications -- that's part of what makes it a
List. If you add elements only at the end and process only from
beginning to end then you get FIFO behavior.

If you want simple FIFO queue, you will have to write it yourself.
Oddly simple LIFO and FIFO queues were not included in the
Collections. They are pretty easy to write. You could do a variant of
Sun's or my LinkedList stripping out the List features.

In the meantime you can use a LinkedList for a FIFO or LIFO queue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top