java中arraylist和一维数组,二维数组的转换

package com.xunfang.epay.util;

import java.util.ArrayList;

public class TwoArray {
	// 数组转换问题

	public static void main(String[] args) {
		String str[][] = { { "a1", "a2", "a3" }, { "b1", "b2" },
				{ "c1", "c2", "c3", "c4" } };

		String arr1[] = { "a1", "a2", "a3" };
		String arr2[] = { "b1", "b2" };
		String arr3[] = { "c1", "c2", "c3", "c4" };
		String strTwo[][] = new String[3][];
		String strone1[];

		ArrayList<String> list1, list2, list3;
		list1 = new ArrayList<String>();
		list1.add("a1");
		list1.add("a2");
		list1.add("a3");

		list2 = new ArrayList<String>();
		list2.add("b1");
		list2.add("b2");

		list3 = new ArrayList<String>();
		list3.add("c1");
		list3.add("c2");
		list3.add("c3");
		list3.add("c4");

		ArrayList<ArrayList<String>> listTwo = new ArrayList<ArrayList<String>>();
		listTwo.add(list1);
		listTwo.add(list2);
		listTwo.add(list3);

		// 转成一维数组
		strone1 = list1.toArray(new String[list1.size()]);
		String strone2[] = list2.toArray(new String[list2.size()]);
		String strone3[] = list3.toArray(new String[list3.size()]);

		// printOne(strone1);

		// 转成二维数组
		strTwo[0] = strone1;
		strTwo[1] = strone2;
		strTwo[2] = strone3;
		printTwo(strTwo);
	}

	private static void printOne(String arr[]) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + ",");
		}
	}

	private static void printTwo(String arr[][]) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + ",");
			}
			System.out.println();
		}
	}
}

你可能感兴趣的:(java中arraylist和一维数组,二维数组的转换)