|
Re: Help with multi dimensional array
|
Posted: Mar 8, 2007 2:49 AM
|
|
If you split the line, then you get a array of Strings.
String[] lineContene = line.split(...)
You could now just create an array like this:
String[][] content = new String[nLines][nColumn];
But you a) don't know how many lines you have and you b) don't know what's the maximum word count is. As a matter of fact, in Java every row in this array could have a different number of columns, but that would cause some problems later.
In my opinion you should start with an Vector.
Vector<String[]> contentTmp = new Vector<String[]>();
//then you read the file and add every array of words to the list.
while (...) {//while reading from the file get's a valid line
contentTmp.add(line.split(...));
}
//Now we calculate the maximum word count
int nWords = 0;
for (String[] lineContent : contentTmp)
nWords = Math.max(nWords, lineContent.length);
String[][] content = new String[contentTmp.size(), nWords);
//Now assign the words
for (int lineNr = 0; lineNr < content.length; lineNr++) {
for (int wordNr = 0; wordNr < contentTmp.get(lineNr).length; wordNr++) {
content[linrNr][wordNr] = contentTmp.get(lineNr)[wordNr]
}
}
I may have made some errors writing this, but it should lead you the right direction.
|
|