You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效拼接2800×1700的float二维数组为单个字符串?

二维大数组拼接为字符串的性能优化咨询

我尝试将一个2800×1700规模的二维float数组所有值拼接为单个字符串,但当前实现性能很差,想请教有没有更优的实现方式?

初始实现代码:

float[][] datas = obj.getMatrix();
String result = "";
int[] shape = {2800,1700};
for(k=0; k<shape[0]; k++){
   for(j=0; j<shape[1]; j++){
      result += (datas[k][j] + "").getBytes();
   }
}

更新1
我正在使用unidata netCDF Java API转换数据以生成ncML响应,希望把float[][]中的值加入列表。我不想生成类似"10.0,15.1,45.6,25.4....."的字符串,而是想把每个值转为byte[],最终得到格式为"[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx...."的字符串,之后通过Base64.getEncoder().encode(result.getBytes())转成Base64编码。

修改后的代码:

float[][] datas = obj.getMatrix();
    StringBuffer result = new StringBuffer();
    int[] shape = {2800,1700};
    for(k=0; k<shape[0]; k++){
       for(j=0; j<shape[1]; j++){
          result.append((datas[k][j] + "").getBytes());
       }
    }
byte[] encodedByte = Base64.getEncoder().encode(result);
String encodedStr = new String(result);

用户获取响应后,可解码得到上述格式的字符串并识别其中的值。


更新2
感谢所有评论,我意识到该方案不可行,最终决定采用result.append(datas[k][j] + ",");的方式实现。

内容的提问来源于stack exchange,提问作者Fab83i

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 11:45:58