简体   繁体   中英

How can i convert double[][] array to String[][] array?

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
String[][] s2d = new String[c.length][c.length];

Should be

String[][] s2d = new String[c.length][c[0].length];

You are going outside of your bound as you are creating a 2x2 matrix instead of a 2x4

Also, Arrays by nature of Java do not have pretty prints. Use Arrays.deepToString to properly output an array. Fixed per @PaulBoddington's comment.

You have several problems with your code, but the one that leads directly to the unexpected output / result is that this ...

               s2d[i][j]=String.valueOf(c);

... should be ...

               s2d[i][j] = String.valueOf(c[i][j]);

You have several problems in this code. Here is a solution, more intuitive than what you have and will releave you from headache.


Explanation

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.


Solution

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;
}

Input

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));
    }
}

Output

[2.0,  3.1,  3.0,  7.0]
[1.5,  5.8,  9.6,  1.0]

Your code is a little strange... for example you are taking in double[][] a as an argument but never use it. By the way, the reason you are getting "address output" is because you are setting every variable in s2d to the String with the value of the array c instead of the single doubles.

To solve your problem you should be using

s2d[i][j] = String.valueOf(a[i][j]); instead of s2d[i][j]=String.valueOf(a);

## ----------


#include <iostream>
#include <string>

using namespace std;
int main()
{
    string line,line0[99];
    double ele[99];
     getline(cin,line);
    for (int x = 0;x <= line.length();x++)
    {
        if (line[x] == ' ')
        {
            step++;
            continue;
        }
        else
        {
            line0[step] += line[x];
        }
    }
    for (int i = 0;i < size_; i++)
    {
        string vo = line0[i];
        ele[i] += stof(vo);
    }
}
```
 ##

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM