简体   繁体   English

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

[英]How to convert array to char array?

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);
    }

I read my file to array, but to continue I need to convert this array to char array. 我将文件读取到数组,但是要继续,我需要将此数组转换为char数组。 Thanks in advance. 提前致谢。

It would be easier to start with List<char[]> variable to shorten the code: 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));

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM