Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ArrayList of ArrayLists - how to get the values out?

Reply
Thread Tools

ArrayList of ArrayLists - how to get the values out?

 
 
news.amnet.net.au
Guest
Posts: n/a
 
      04-23-2004
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).ge t(j).toString() +
"<br/>");
}
}

Any help will be greatly appreciated.

Thanks very much

Hugo


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-23-2004
On Fri, 23 Apr 2004 10:32:18 +0800, "news.amnet.net.au"
<(E-Mail Removed)> wrote or quoted :

>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


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Bjorn Abelli
Guest
Posts: n/a
 
      04-23-2004

"news.amnet.net.au" wrote...

> 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


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      04-23-2004
news.amnet.net.au wrote:

> 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).ge t(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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Jason Bell
Guest
Posts: n/a
 
      04-23-2004
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




 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      04-23-2004
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-23-2004
On Fri, 23 Apr 2004 10:38:29 -0500, "John C. Bollinger"
<(E-Mail Removed)> wrote or quoted :

>
>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.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic array/ arraylist of arraylists standshik Java 2 02-11-2006 05:13 PM
filling arraylist with arraylists in loop problem drdave@canoemail.com ASP .Net 2 12-13-2005 07:12 PM
ArrayList of ArrayLists: How to implement IEnumerable =?Utf-8?B?UG9udGlNYXg=?= ASP .Net 0 07-05-2004 02:12 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments