简体   繁体   English

连接多个arraylists引用

[英]Concatenate multiple arraylists references

So I have 3 arraylists called for example books , newspapers , magazines and one arraylist called resources . 因此,我有3个数组列表,例如booksnewspapersmagazines和一个数组列表,称为resources

If you do something like : resources = books; 如果您执行以下操作: 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? 但是,如何在其他两个arraylist的末尾添加此resources ,以使资源包含所有arraylist? Something like concatenation of arraylists memories references. 类似于串联数组列表内存引用。

  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. 如果您希望书籍,报纸和杂志的元素ArrayLists出现在一个ArrayList resources ,则可以使用ArrayList.addAll(Collection)函数:将指定集合中的所有元素追加到此列表的末尾,在它们由指定集合的​​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 ,其中将ArrayLists作为元素:

      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> . 假设书籍,杂志和报纸的ArrayLists的类型为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>>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM