简体   繁体   中英

Insert data into 2d array from file via split?

How could one populate a 2d array from a text file using split?

String proxies[][] = {{"127.0.0.1","80"}, {"127.0.0.1","443"}, {"127.0.0.1","3306"}};

In my text file I have data with a ip:port on each line:

127.0.0.1:80
127.0.0.1.443
127.0.0.1.3306

I could populate a 1d array using split like this:

proxies = everyLine.split("\\n");

How would I insert the ip:port data into a 2d array?

Using Java constructs it's not possible. You can use Apache Commons method FileUtils#lineIterator(File, String) to iterate over lines and apply String.split(String) on each

    String[] lines = everyLine.split("\\n");
    String[][] proxies = new String[lines.length][];
    int i=0;
    for ( String line : lines )
    {
        proxies[i++] = line.split(":");
    }

you could split on the : operator.

String []proxies = everyLine.split("\\n");
for(int i=0;i<proxies.length;i++){
 String[] anotherDimention= proxies[i].split(":");
// do something useful with it
}

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