如何将数组转换为char数组?

时间:2014-06-04 20:48:18

标签: java arrays

public static void main(String[] args) throws IOException {

    String token1 = "";
    Scanner inFile = new Scanner(new File(filename));
    List<String> temps = new LinkedList<String>();

    while (inFile.hasNext()) {
      token1 = inFile.next();
      temps.add(token1);
    }

    inFile.close();

    String[] tempsArray = temps.toArray(new String[0]);

    for (String hole : tempsArray) {
        System.out.println(hole);
    }

我将文件读取到数组,但要继续,我需要将此数组转换为char数组。 提前致谢。

2 个答案:

答案 0 :(得分:2)

List<char[]>变量开始缩短代码会更容易:

Scanner inFile = new Scanner(new File(filename));
List<char[]> temps = new ArrayList<char[]>();
while (inFile.hasNext()) {
    temps.add(inFile.nextLine().toCharArray());
}
inFile.close();
char[][] wholeData = temps.toArray(new char[0][]);
//just to show the data
System.out.println(Arrays.deepToString(wholeData));

答案 1 :(得分:0)

string的toCharArray()方法将返回字符串长度为char []的数组。