배열(Array) -> ArrayList 변환

String[] arr = new String[5];

arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
arr[3] = "four";
arr[4] = "five";

ArrayList<String> arrList = new ArrayList<>();

for(String k : arr) {
	arrList.add(k);
}

+ ) Arrays 메서드 사용

String[] arr = new String[5];

arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
arr[3] = "four";
arr[4] = "five";

ArrayList<String> arrList = new ArrayList<>(Arrays.asList(arr));

 

ArrayList -> 배열(Array)

ArrayList<String> arrList = new ArrayList<>();

arrList.add("one");
arrList.add("two");
arrList.add("three");
arrList.add("four");
arrList.add("five");

String[] arr = new String[arrList.size()];
int size = 0;
for(String k : arrList) {
	arr[size++] = k;
}

+ ) List 메서드 사용

ArrayList<String> arrList = new ArrayList<>();

arrList.add("one");
arrList.add("two");
arrList.add("three");
arrList.add("four");
arrList.add("five");

String[] arr = arrList.toArray(new String[arrList.size()]);

+ Recent posts