将 3 个数组列表合并为一个 [英] Merge 3 arraylist to one

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

问题描述

我想在 Java 中将 3 个数组列表合并为一个.有谁知道做这种事情的最好方法是什么?

I want to merge down 3 arraylist in one in java. Does anyone know which is the best way to do such a thing?

推荐答案

使用 ArrayList.addAll().这样的事情应该可以工作(假设列表包含 String 对象;您应该相应地更改).

Use ArrayList.addAll(). Something like this should work (assuming lists contain String objects; you should change accordingly).

List<String> combined = new ArrayList<String>();
combined.addAll(firstArrayList);
combined.addAll(secondArrayList);
combined.addAll(thirdArrayList);

更新

我可以从您的评论中看出您实际上可能正在尝试创建一个 2D 列表.如果是这样,则应使用以下代码:

Update

I can see by your comments that you may actually be trying to create a 2D list. If so, code such as the following should work:

List<List<String>> combined2d = new ArrayList<List<String>>();
combined2d.add(firstArrayList);
combined2d.add(secondArrayList);
combined2d.add(thirdArrayList);

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

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