连接多个数组列表引用 [英] Concatenate multiple arraylists references

查看:23
本文介绍了连接多个数组列表引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有 3 个数组列表,例如 booksnewspapersmagazines 和一个名为 resources 的数组列表.

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

如果你这样做:resources = books; 那么资源将指向与书籍相同的内存位置.

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

但是如何在其他两个数组列表的开头添加到此 resources 中,以便资源将包含所有数组列表?类似于数组列表内存引用的串联.

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.

推荐答案

  1. 如果你想让书籍、报纸和杂志的元素ArrayLists出现在一个ArrayList resources中,那么你可以ArrayList.addAll(Collection) 函数的使用:按照指定集合的​​迭代器返回的顺序,将指定集合中的所有元素追加到该列表的末尾.

  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);

  • 您想创建一个 ArrayList,它将包含 ArrayLists 作为元素:

  • 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);
    

  • 假设书籍、杂志和报纸的类型ArrayListsArrayList.但是,如果第二个是您的目标,那么我认为最好将资源声明为 HashMap>.

    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);
    

    这篇关于连接多个数组列表引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆