0

So I have 3 arraylists called for example books, newspapers, magazines and one arraylist called resources.

If you do something like : resources = books; then resources will point to the same memory location as books.

But how can you add to this resources at the end the start of the other two arraylists so the resources will contain all arraylists? Something like concatenation of arraylists memories references.

3
  • You really can't glue together different types unless they share a common ancestory, if newspaper is not the same type as books. Commented Nov 16, 2013 at 17:51
  • Can you share some coding that How u initialize your arraylists... Commented Nov 16, 2013 at 17:51
  • ArrayList<Resource> books = new ArrayList(); ArrayList<Resource> magazines = new ArrayList(); ArrayList<Resource> newspapers = new ArrayList(); Actually I have an abstract class called Resource and three extended classes Book, Newspaper, Magazine. I need an efficient way to iterate through all arrays. Commented Nov 16, 2013 at 18:05

1 Answer 1

1
  1. If you want the elements of book, newspaper and magazine ArrayLists will appear in one ArrayList resources, then you can make use of ArrayList.addAll(Collection) function: Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

    resources.addAll(book);
    resources.addAll(newspaper);
    resource.addAll(magazine);
    
  2. You want to create an ArrayList which will contain the ArrayLists as element:

     ArrayList<ArrayList<String>>resources = new ArrayList<>();
     resources.add(book);
     resources.add(newspaper);
     resources.add(magazine);
    

Assumed that the type of book, magazine and newspaper ArrayLists is ArrayList<String>. However, if second one is your target then i think it is better you declare resources as an HashMap<String, ArrayList<String>>.

    HashMap<String, ArrayList<String>>resources = new HashMap<>();
            // replace String with the type of the elements of book\newspaper\magazine
    resources.put("book", book);
    resources.put("newspaper", newspaper);
    resources.put("magazine", magazine);
Sign up to request clarification or add additional context in comments.

2 Comments

This way I will consume memory unnecessary.
which way ? I have mentioned three ways, i think. And i don't see any one of this will consume memory unnecessarily, at the very least last two, because they will save your array List as element with same reference. Anything you change to outer array list, for example book, it will be reflected on the ArrayList book added to resources or the HashMap implementation. Give the last two some try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.