简体   繁体   中英

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. Thanks in advance.

It would be easier to start with List<char[]> variable to shorten the code:

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 []数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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