arrays - Java, How to split elements of a list? -
i'm trying split individual elements of string[]
array. array looks
{"0 1 0 1", "0 0 1 1", "1 1 0 1",...}.
i want split each element regex " ", single space. if that, create 1 element "0 1 0 1" 4 elements "0", "1", "0", , "1"? here code adds elements list (reading text file rows of 4 digit numbers separated spaces) part using "split" method not working. not having impact on array produced. name of string[]
array "splitted".
int j=0; string thisline = null; while(((thisline=readthrough.readline()) != null) && j<3){ splitted[j]= thisline; splitted[j].split(" "); j++; }
the split()
method doesn't modify string in-place, have to assign returned string[]
something. if want process values, seek this:
string[] array = {"0 1 0 1", "0 0 1 1", "1 1 0 1"}; list<string> reply = new arraylist<string>(); (string str : array) (string s : str.split(" ")) answer.add(s);
after loop runs, answer
contain next string values:
[0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1]
optionally, if need output result array of strings can this:
string[] output = answer.toarray(new string[answer.size()]);
java arrays
No comments:
Post a Comment