数组和List 互相转换 List to Array

数组和List 相互转换, 可以是任意类型的数组

package com.ljc.java.list;

import org.junit.Test;
import java.util.*;

import static org.junit.Assert.assertEquals;

/**
 * Created with IntelliJ IDEA.
 * User: Liu_jc
 * Date: 13-6-20
 * Time: 上午9:59
 * To change this template use File | Settings | File Templates.
 */
public class ListToArrayTest {
    private String[] strArray = {"aa", "bb", "cc"};

    @Test
    public void testListArray() throws Exception {

        // Array to List
        List<String> list = Arrays.asList(strArray);
        assertEquals(strArray.length, list.size());
        assertEquals("aa", list.get(0));
        assertEquals(strArray[0], list.get(0));

       // List to Array
        String[] newStrArray = list.toArray(new String[]{});
        assertEquals(list.size(), newStrArray.length);
        assertEquals("aa", list.get(0));
    }
}

你可能感兴趣的:(java,List to Array,Array to List,List转换成数组,数据转换成List)