从数组列表到数组 [英] From Arraylist to Array

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

问题描述

我想知道从 ArrayList 转换为 Array 是否安全/可取?我有一个文本文件,每行一个字符串:

I want to know if it is safe/advisable to convert from ArrayList to Array? I have a text file with each line a string:

1236
1233
4566
4568
....

我想将它们读入数组列表,然后将其转换为数组.这样做是否明智/合法?

I want to read them into array list and then i convert it to Array. Is it advisable/legal to do that?

谢谢

推荐答案

ArrayList 转换为 Array 是安全的.这是否是一个好主意取决于您的预期用途.是否需要ArrayList 提供?如果是这样,请将其保留为 ArrayList.否则转换掉!

Yes it is safe to convert an ArrayList to an Array. Whether it is a good idea depends on your intended use. Do you need the operations that ArrayList provides? If so, keep it an ArrayList. Else convert away!

ArrayList<Integer> foo = new ArrayList<Integer>();
foo.add(1);
foo.add(1);
foo.add(2);
foo.add(3);
foo.add(5);
Integer[] bar = foo.toArray(new Integer[foo.size()]);
System.out.println("bar.length = " + bar.length);

输出

bar.length = 5

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

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