i have a text file contains this:
1 1 0 0
1 2 0 0
1 0 1 0
1 0 2 0
1 0 0 1
1 0 0 2
2 1 0 0
2 2 0 0
2 0 1 0
2 0 2 0
2 0 0 1
2 0 0 2
then, i put the contents in an arraylist. the result will be same as in file. next, i want to process the data 1 by 1 and put each row of the content in an array[][] where the the data will be seperated by row. the result will be like this:
output[0][]={1 1 0 0}
开发者_JAVA技巧output[1][]={1 2 0 0}
output[2][]={1 0 1 0}
output[3][]={1 0 2 0}
....
....
....
question, how can i take the string in arraylist to become a separated data? i code in java
thanks
you can use " public String[] split(String regexp)" method to split string in to array by specifying character by which you split in argument. e.g.String temp = "1 2 3 4"; temp.split(" "); you will split by blank space in your case..
As @Benoit already stated, you can split each line using String#split(regex)
, like this:
String line = ...;
String[] parts = line.split( "\\s+" ); //split on whitespace
Note that leading whitespace might result in empty strings at the beginning, i.e. " 1 2 3 4 " would result in {"", "1", "2", "3", "4"}
. You might also use the Apache Commons Lang class StringUtils
whose split(...)
method takes care of that.
Also note the expression \s+
which would also split on multiple whitespacem, i.e. "1 2 3 4" would still result in {"1", "2", "3", "4"}
.
You then might parse the individual parts as integers etc.
Here's some really simple code that does all the work in a couple of lines using Scanners, and handles any number of numbers per line.
EDITED: Note return type of List<List<Integer>>
chosen for sanity over int[][]
public static List<List<Integer>> parseIntArrays(InputStream in) {
Scanner s = new Scanner(in);
List<List<Integer>> list = new ArrayList<List<Integer>>();
while (s.hasNextLine()) {
Scanner ns = new Scanner(s.nextLine());
List<Integer> nums = new ArrayList<Integer>();
list.add(nums);
while (ns.hasNextInt())
nums.add(ns.nextInt());
}
return list;
}
Here's some test code for you executing pleasure:
public static void main(String[] args) {
String input = "1 0 0\n1 2 0 0\n1 0 1 0\n1 0 2 0 0 0 1\n1";
List<List<Integer>> result = parseIntArrays(new ByteArrayInputStream(input.getBytes()));
for (List<Integer> line : result)
System.out.println(line);
}
Output:
[1, 0, 0]
[1, 2, 0, 0]
[1, 0, 1, 0]
[1, 0, 2, 0, 0, 0, 1]
[1]
Question is: Why are you using a 2 dimensional array to store the data? Parse the file by new line. For each new line, add that line to the array list. For each element in the array list, just add it to the array. I wrote a simple program. It assumes you've already parsed the file and have populated the ArrayList with new lines.
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1 0 1 1");
list.add("1 1 1 1");
// etc
String[][] array = new String[list.size()][list.size()];
int i = 0;
for (String s : list) {
stringArray[i++][0] = s;
}
for (int y = 0 ; y < array.length; y++) {
System.out.println(stringArray[y][0]);
}
}
精彩评论