Initialising An ArrayList
This question already has an answer here:
This depends on what you mean by initialize. To simply initialize the variable time with the value of a reference to a new ArrayList , you do
ArrayList<String> time = new ArrayList<String>();
(replace String with the type of the objects you want to store in the list.)
If you want to put stuff in the list, you could do
ArrayList<String> time = new ArrayList<String>();
time.add("hello");
time.add("there");
time.add("world");
You could also do
ArrayList<String> time = new ArrayList<String>(
Arrays.asList("hello", "there", "world"));
or by using an instance initializer
ArrayList<String> time = new ArrayList<String>() {{
add("hello");
add("there");
add("world");
}};
Arrays.asList allows you to build a List from a list of values.
You can then build your ArrayList by passing it the read-only list generated by Arrays.asList .
ArrayList time = new ArrayList(Arrays.asList("a", "b", "c"));
But if all you need is a List declared inline, just go with Arrays.asList alone.
List time = Arrays.asList("a", "b", "c");
ArrayList<String> time = ArrayList.class.newInstance();
链接地址: http://www.djcxy.com/p/27712.html
上一篇: 给List添加值<String> = new ArrayList <String>();
下一篇: 初始化ArrayList
