I am working with matrix in java. ( another story 🙂 )
I want to read a CSV file and store it in a variable. I will manipulate values then again store it in CSV file. I used STRING as data type. But if CSV file has like 500 columns. It kill my program speed :(. I think this is not good data type. Which data type I can use to temporary store LONG TEXT?
If my question is not clear please ask questions. I will explain.
Thanks
P.S: I am reading one line and storing it in variable like this
String str;
str += read line by line from CSV;
here is the loop
String reduceM="";
for(int kk=0;kk<W2.getRowDimension();kk++){
for(int jj=0;jj<W2.getColumnDimension();jj++){
reduceM += Double.toString(reduceMatrix[kk][jj]);
}
System.out.println("\r\n");
}
Use a
StringBuilder(orStringBufferif you’re using Java 1.5 or older):This will avoid it creating a new (and increasingly long) string for each iteration of the two loops.
However, there are no commas or line-breaks in this code – I suspect you actually want something like this:
Note that that will leave an extra comma at the end of each row – let me know if you want ideas of how to remove that.
See this article for why this could make a huge improvement to your running time. (Note that it’s an old article, talking about
StringBufferrather thanStringBuilder– the latter is just an unsynchronized version of the former.)