Nico Hoppel Nico Hoppel - 26 days ago 8
Java Question

Change value of array

I have this code:

List<String[]> list = csvReader.readAll();
// Convert to 2D array
String[][] dataArr = new String[list.size()][];


Now I want to replace some char at specific values.

dataArr = list.toArray(dataArr);
String help = Arrays.toString(dataArr[0]).replace("[", "");
dataArr[0] = help;


But I get this error:


incompatible types: String cannot be converted to String[]


My question is, how can I fix this?

Edit:

Wow, thanks for all the input.

I have to be a little bit more specific and this is my input file:


[0,0,0,1,1]

[1,1,1,1,1]

[0,0,0,0,0]

[1,0,1,0,1]

[0,1,0,1,0]


This line:

System.out.println(Arrays.deepToString(dataArr));


leads to following output:


[[[0, 0, 0, 1, 1]], [[1, 1, 1, 1, 1]], [[0, 0, 0, 0, 0]], [[1, 0, 1, 0, 1]], [[0, 1, 0, 1, 0]]]


I want to replace the double "[[" and "]]" to "[" and "]".

Answer

to replace all the "[[" you can simply use

String output = Arrays.deepToString(dataArr);
output = output.replaceAll("\\[\\[","\\[");
output = output.replaceAll("\\]\\]","\\]");
output = output.substring(2, output.length()-2);
String[] matrix = output.split("\\],\\[");

And that should do it