连接多个数组列表引用 [英] Concatenate multiple arraylists references
问题描述
所以我有 3 个数组列表,例如 books
、newspapers
、magazines
和一个名为 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.
推荐答案
如果你想让书籍、报纸和杂志的元素
ArrayLists
出现在一个ArrayList
resources
中,那么你可以ArrayList.addAll(Collection)
函数的使用:按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到该列表的末尾.
If you want the elements of book, newspaper and magazine
ArrayLists
will appear in oneArrayList
resources
, then you can make use ofArrayList.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);
假设书籍、杂志和报纸的类型ArrayLists
是ArrayList
.但是,如果第二个是您的目标,那么我认为最好将资源声明为 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屋!