简体   繁体   中英

How to split an arraylist into two dimensional arrays?

I am reading a text file into an arraylist and getting them by line but I want to split each line and put in a two dimensional array however String [][] array=lines.split(","); gives me an error.

File file=new File("text/file1.txt");
ArrayList<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array=lines.split(",");

You must split each element of the List separately, since split operates on a String and returns a 1-dimensional String array :

File file=new File("text/file1.txt");
ArrayList<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array=new String[lines.size()][];
for (int i=0;i<lines.size();i++)
    array[i]=lines.get(i).split(",");

split() returns [] not [][]. try this :

File file=new File("text/file1.txt");
List<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array= new String[lines.size()][];
int index = 0;
for (String line : lines) {
    array[index] = line.split(",");
    index++;
}

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