Java学习笔记51:数组转ArrayList和ArrayList转数组技巧

时间:2022-03-26 10:19:38

ArrayList转数组:

        public class Test {

            public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("王利虎");
list.add("张三");
list.add("李四");
int size = list.size();
String[] array = (String[]) list.toArray(new String[size]);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}

数组转ArrayList:

  public class Test {

            public static void main(String[] args) {
String[] array = new String[3];
array[0] = "王利虎";
array[1] = "张三";
array[2] = "李四";
List<String> list = Arrays.asList(array);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
} }
}