Hello,
I created an ArrayList with lists and arrays (randomly chosen) of queues and now need to get access to these lists and arrays to add, remove and display elements in them. How could I do this? I tried ArrayList methods like get(int index) and iterator(), but it just shows me what kind of Objects are in the ArrayList and does not let me use queue classes methods on them.

Anna

You have to cast them to the appropriate type if you wish to call methods on them. To call ArrayList methods on an Object you retrieved you would do this

ArrayList retrievedList = (ArrayList) otherBigList.get(0);
retrievedList.someListMethod();

That's the technical aspect of it. The bigger question is why are you storing a mixed bag of stuff in a single ArrayList? Generally it's more useful to put things that share some specific interface into a collection. The nature of that interface depends on what you expect to do with them. While you can throw all kinds of things into a collection as just Objects and sometimes there are reasons to do that, you really should consider what you expect to do with them first and type the collection to the interface you need to work with.

Thank you very much!

The reason is simple: this is what my assignment brief is telling me to do ;) It says 'This will allow to practice with ArrayList and allow you to use generics, if you wish.'

The problem is that it's the first time I am using ArrayList and could not find enough information about it.

This is what I get now :(

'Exception in thread "main" java.lang.ClassCastException: AQueue cannot be cast to java.util.ArrayList'

I can cast it to FIFO, which is my interface, and retrieve it to a FIFO type object, but this won't give me all the methods I need :(

This is what I get now :(

'Exception in thread "main" java.lang.ClassCastException: AQueue cannot be cast to java.util.ArrayList'

I can cast it to FIFO, which is my interface, and retrieve it to a FIFO type object, but this won't give me all the methods I need :(

Too bad. You can's cast something to something it isn't, period.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.