JAVA中list与数组相互转换及数组去重

List转数组:

List list = new ArrayList<>();

list.add("1");

list.add("2");

String[] strings = new String[list.size()];

list.toArray(strings);


数组转list:

(1) list不可加减

String[] studentIds = new String[] {"1","2"};

List list = Arrays.asList(studentIds);

(2)list可加减

String[] studentIds = new String[] {"1","2"};

List list = new ArrayList<>();

Collections.addAll(list, studentIds);

list.add("3");

list.remove("2");


数组去重:

String[] studentIds = new String[] {"1","1","2"};

List newStudentIds = new ArrayList<>();

for(String studentId : studentIds) {

//过滤掉重复的

if(!newStudentIds.contains(studentId)) {

newStudentIds.add(studentId);

}

}

String[] array =new String[newStudentIds.size()];

newStudentIds.toArray(array);

你可能感兴趣的:(JAVA中list与数组相互转换及数组去重)