java 将 double[] 数组转换为 string[] 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11353768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:45:08  来源:igfitidea点击:

Converting a double[] array into string[] array

javaarraystype-conversion

提问by MBC870

I would like to know what code to use to convert a double[] array into a string[] array

我想知道使用什么代码将 double[] 数组转换为 string[] 数组

回答by WhyNotHugo

You'll need to create a target array of equal size to the original array, and iterate over it, converting element by element.

您需要创建一个与原始数组大小相同的目标数组,并对其进行迭代,逐个元素地进行转换。

Example:

例子:

double[] d = { 2.0, 3.1 };
String[] s = new String[d.length];

for (int i = 0; i < s.length; i++)
    s[i] = String.valueOf(d[i]);

回答by marc wellman

As already mentioned you have to iterate and convert every item from double to String.

如前所述,您必须迭代并将每个项目从双精度转换为字符串。

Alternatively it's also possible to avoid an explicit iteration and do the following:

或者,也可以避免显式迭代并执行以下操作:

// source array
Double[] d_array = new Double[] { 1, 2, 3, 4 };

// create a string representation like [1.0, 2.0, 3.0, 4.0]
String s = Arrays.toString(d_array);

// cut off the square brackets at the beginning and at the end
s = s.substring(1, s.length - 1);

// split the string with delimiter ", " to produce an array holding strings
String[] s_array = s.split(", ");