LoginSignup
33
29

More than 5 years have passed since last update.

List と 配列の相互変換

Last updated at Posted at 2014-01-20
変換前 変換後
List<T> T[]
List<Integer> int[]
T[] List<T>
int[] List<Integer>

基本型のラッパクラスの List は 基本型の配列に変換する。
もっと楽な方法ないのかな。

ConvertListArray.java
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConvertListArray {
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4));
        int[] arr = new int[]{0,1,2,3,4};
        ArrayList<Character> list2 = new ArrayList<Character>(Arrays.asList('h','e','l','l','o'));
        Character[] arr2 = {'h','e','l','l','o'};

        // List<Integer> -> int[]
        if (Arrays.equals(toArr(list),arr)) System.out.println("OK");
        else System.out.println("not OK");

        // List<T> -> T[]
        if (Arrays.equals(list2.toArray(new Character[0]),arr2)) System.out.println("OK");
        else System.out.println("not OK");

        // int[] -> List<Integer>
        if (toList(arr).equals(list)) System.out.println("OK");
        else System.out.println("not OK");

        // T[] -> List<T>
        if (new ArrayList<Character>(Arrays.asList(arr2)).equals(list2)) System.out.println("OK");
        else System.out.println("not OK");
    }   

    public static int[] toArr(List<Integer> list){
        // List<Integer> -> int[]
        int l = list.size();
        int[] arr = new int[l];
        Iterator<Integer> iter = list.iterator();
        for (int i=0;i<l;i++) arr[i] = iter.next();
        return arr;
    }   

    public static ArrayList<Integer> toList(int[] arr){
        // int[] -> ArrayList<Integer>
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int t : arr) list.add(t);
        return list;
    }
}

追記

Listと配列の相互変換は簡潔に書けるようになった。
わざわざメソッドにしなくてもいいな。
ただ、ジェネリクスを使った List から 配列への変換はまだ分からない。

// List<T> list -> T[]
list.toArray(new T[0]) // これはエラー。汎用配列は生成できない
list.toArray(new Integer[0]) // Integer 型の場合。

// T[] arr -> List<T>
// 指定された配列に連動する固定サイズのリスト。返されたリストへの変更は、そのまま配列に書き込まれる
Arrays.asList(arr) 
// ArrayList<T> にする場合
new ArrayList<T>(Arrays.asList(arr))
import java.util.ArrayList;
import java.util.Arrays;

public class ConvertListArray {
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4));
        Integer[] arr = {0,1,2,3,4};

        // List<T> -> T[]
        if (Arrays.equals(list.toArray(new Integer[0]),arr)) System.out.println("OK");
        else System.out.println("not OK");

        // T[]  -> List<T>
        if ( new ArrayList<Integer>(Arrays.asList(arr)).equals(list)) System.out.println("OK");
        else System.out.println("not OK");
    }   
}

追記(2)

List<T> list -> T[]
汎用配列の初期化は難しいので、引数に配列を渡してそこにコピーする。

import java.util.ArrayList;

public class ConvertListArray {
    public static void main(String[] args){
        // 使用例
        ArrayList<Character> list = new ArrayList<Character>(Arrays.asList('h','e','l','l','o'));
        Character[] arr = new Character[list.size()];
        toArr(list,arr);
    }

    public static <T> void toArr(List<T> list,T[] arr){
        // List<T> -> T[]
        // arr に list をコピー
        list.toArray(arr);
    }
}
33
29
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
29