I work on it but i can't do that.This code has address output i want to convert to string array from double array. What is wrong with this code.
public static String[][] getStrings(double[][] a) {
double[][] c = { {2.0, 3.1,3,7}, {1.5,5.8,9.6,1} };
String[][] s2d = new String[c.length][c.length];
for (int i = 0; i < s2d.length; i++) {
for(int j=0; j<s2d[i].length;j++){
s2d[i][j]=String.valueOf(c);
System.out.println(s2d);
}
}
return s2d;
}
This is the output that i have:
[[Ljava.lang.String;2a139a55
[[Ljava.lang.String;2a139a55
[[Ljava.lang.String;2a139a55
[[Ljava.lang.String;2a139a55
[[Ljava.lang.String;2a139a55
You have several problems in this code. Here is a solution, more intuitive than what you have and will releave you from headache.
Within your method, you create a new String
array of arrays with as much arrays as contained in your double[][]
parameter. As the sizes of each array can vary, we are not initializing it.
new String[a.length][];
Then, we loop over all the arrays of doubles, convert them to String
using Arrays#toString()
(we trim the "[" "]"
that are the first and last character of the output of this method) and split it to have an array of Strings.
public static String[][] getStrings(double[][] a) {
String[][] output = new String[a.length][];
int i = 0;
for (double[] d : a){
output[i++] = Arrays.toString(d).replace("[", "").replace("]", "").split(",");
}
return output;
}
public static void main(String[]a) {
double[][] ds = { { 2.0, 3.1, 3, 7 }, { 1.5, 5.8, 9.6, 1 } };
for (String[] s : getStrings(ds)){
System.out.println(Arrays.toString(s));
}
}
[2.0, 3.1, 3.0, 7.0]
[1.5, 5.8, 9.6, 1.0]