How to store arrayList into an array in java?
如何将arrayList存储到java中的数组中?
7 个解决方案
#1
12
That depends on what you want:
这取决于你想要的:
List<String> list = new ArrayList<String>();
// add items to the list
Now if you want to store the list in an array, you can do one of these:
现在,如果要将列表存储在数组中,可以执行以下操作之一:
Object[] arrOfObjects = new Object[]{list};
List<?>[] arrOfLists = new List<?>[]{list};
But if you want the list items in an array, do one of these:
但是,如果您想要数组中的列表项,请执行以下操作之一:
Object[] arrayOfObjects = list.toArray();
String[] arrayOfStrings = list.toArray(new String[list.size()]);
Reference:
参考:
Collection.toArray()
- Collection.toArray()
Collection.toArray(T[])
- Collection.toArray(T [])
#2
4
If Type is known (aka not a generics parameter) and you want an Array of Type:
如果Type已知(也就是非泛型参数)并且您想要一个Type类型:
ArrayList<Type> list = ...;
Type[] arr = list.toArray(new Type[list.size()]);
Otherwise
除此以外
Object[] arr = list.toArray();
#3
2
You mean you want to convert an ArrayList
to an array?
你的意思是你想将ArrayList转换为数组?
Object[] array = new Object[list.size()];
array = list.toArray(array);
Choose the appropriate class.
选择合适的班级。
#4
0
List list = getList();
Object[] array = new Object[list.size()];
for (int i = 0; i < list.size(); i++)
{
array[i] = list.get(i);
}
Or just use List#toArray()
或者只使用List#toArray()
#5
0
List<Foo> fooList = new ArrayList<Foo>();
Foo[] fooArray = fooList.toArray(new Foo[0]);
#6
0
List list = new ArrayList();
list.add("Blobbo");
list.add("Cracked");
list.add("Dumbo");
// Convert a collection to Object[], which can store objects
Object[] ol = list.toArray();
#7
0
Try the generic method List.toArray()
:
尝试通用方法List.toArray():
List<String> list = Arrays.asList("Foo", "Bar", "Gah");
String array[] = list.toArray(new String[list.size()]);
// array = ["Foo", "Bar", "Gah"]
#1
12
That depends on what you want:
这取决于你想要的:
List<String> list = new ArrayList<String>();
// add items to the list
Now if you want to store the list in an array, you can do one of these:
现在,如果要将列表存储在数组中,可以执行以下操作之一:
Object[] arrOfObjects = new Object[]{list};
List<?>[] arrOfLists = new List<?>[]{list};
But if you want the list items in an array, do one of these:
但是,如果您想要数组中的列表项,请执行以下操作之一:
Object[] arrayOfObjects = list.toArray();
String[] arrayOfStrings = list.toArray(new String[list.size()]);
Reference:
参考:
Collection.toArray()
- Collection.toArray()
Collection.toArray(T[])
- Collection.toArray(T [])
#2
4
If Type is known (aka not a generics parameter) and you want an Array of Type:
如果Type已知(也就是非泛型参数)并且您想要一个Type类型:
ArrayList<Type> list = ...;
Type[] arr = list.toArray(new Type[list.size()]);
Otherwise
除此以外
Object[] arr = list.toArray();
#3
2
You mean you want to convert an ArrayList
to an array?
你的意思是你想将ArrayList转换为数组?
Object[] array = new Object[list.size()];
array = list.toArray(array);
Choose the appropriate class.
选择合适的班级。
#4
0
List list = getList();
Object[] array = new Object[list.size()];
for (int i = 0; i < list.size(); i++)
{
array[i] = list.get(i);
}
Or just use List#toArray()
或者只使用List#toArray()
#5
0
List<Foo> fooList = new ArrayList<Foo>();
Foo[] fooArray = fooList.toArray(new Foo[0]);
#6
0
List list = new ArrayList();
list.add("Blobbo");
list.add("Cracked");
list.add("Dumbo");
// Convert a collection to Object[], which can store objects
Object[] ol = list.toArray();
#7
0
Try the generic method List.toArray()
:
尝试通用方法List.toArray():
List<String> list = Arrays.asList("Foo", "Bar", "Gah");
String array[] = list.toArray(new String[list.size()]);
// array = ["Foo", "Bar", "Gah"]