首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将数字序列保存到数组中

将数字序列保存到数组中
EN

Stack Overflow用户
提问于 2018-06-01 02:09:08
回答 1查看 213关注 0票数 -2

有一个用于求解Lorentz系统的代码,我需要在每次迭代时将所有的解x,y,z保存在相应的数组解决方案中,也就是说,将5000个解x,y,z放入一个数组中,怎么做才是最好的?那么如何将数组转换为字符串呢?

代码语言:javascript
复制
public class Butterfly {

public static double dx(double x, double y, double z) {
    return -10*(x - y);
}

public static double dy(double x, double y, double z) {
    return -x*z + 28*x - y;
}

public static double dz(double x, double y, double z) {
    return x*y - 8*z/3;
}


public static void main(String[] args) {
    double x = 0.0, y = 20.0, z = 25.0; //
    double dt = 0.001;



    // uses Euler method
    for (int i = 0; i < 5000; i++) {


        // 
        double xnew = x + dx(x, y, z) * dt;
        double ynew = y + dy(x, y, z) * dt;
        double znew = z + dz(x, y, z) * dt;
        x = xnew; 
        y = ynew;
        z = znew;
        double[][] xyzArray = new double[5000][3];
        for (i = 0; i < xyzArray.length; i++) {
            for (int j = 0; i < xyzArray.length; j++) {
                xyzArray[i][j] = x;

            }
        }
        for (i = 0; i < xyzArray.length; i++) {
            for (int j = 0; i < xyzArray.length; j++) {
                System.out.println(xyzArray[i][j]);
            }
            System.out.println();
        }
       }
   }
}

А在编辑代码后,程序显示一个值x,y,z的5000次,而不是所有的值。我认为这样就失去了其他的解决方案。毕竟,我有每个变量的5000个决策,我需要保存每个变量…

代码语言:javascript
复制
 // uses Euler method
       double[][] xyzArray = new double[5000][3];
       for (int i = 0; i < xyzArray.length; i++) {
            for (i = 0; i < 5000; i++) {

            double xnew = x + dx(x, y, z) * dt;
            double ynew = y + dy(x, y, z) * dt;
            double znew = z + dz(x, y, z) * dt;

            xyzArray[i][0] = xnew;
            xyzArray[i][1] = ynew;
            xyzArray[i][2] = znew;
        }
    }
    for (int i = 0; i < xyzArray.length; i++) {
        System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] + ", " + xyzArray[i][2]);

    }

}

编辑后,将生成以下代码。第一次迭代的5,000个解决方案仍然是输出的:它不能在IDEAS或jshell中工作。我知道代码是正确的,但我不明白为什么我得到了错误的结果

代码语言:javascript
复制
public class Butterfly {

public static double dx(double x, double y, double z) {
    return -10 * (x - y);
}

public static double dy(double x, double y, double z) {
    return -x * z + 28 * x - y;
}

public static double dz(double x, double y, double z) {
    return x * y - 8 * z / 3;
}


public static void main(String[] args) {
   double x = 0.0, y = 20.0, z = 25.0;
   double dt = 0.001;
   double[][] xyzArray = new double[5000][3];
   for (int i = 0; i < xyzArray.length; i++) {
        double xnew = x + dx(x, y, z) * dt;
        double ynew = y + dy(x, y, z) * dt;
        double znew = z + dz(x, y, z) * dt;
        xyzArray[i][0] = xnew;
        xyzArray[i][1] = ynew;
        xyzArray[i][2] = znew;
   }
   for (int i = 0; i < xyzArray.length; i++) {
        System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] + ", " + xyzArray[i][2]);


    }

 }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 03:28:33

您已经在循环中声明了数组。您不希望为包含5000个数据点的数组创建5000个副本!您只需要一个包含5000个数据点的数组,因此需要在循环外部声明和创建该数组。

代码语言:javascript
复制
double[][] xyzArray = new double[5000][3];

// uses Euler method
for (int i = 0; i < xyzArray.length; i++) {

    // ... compute xnew, ynew, znew ... etc ... 

获得当前步骤的新值后,需要将它们保存在数组的[i]-th条目中。您将把x存储在[i][0]子项中,将y存储在[i][1]子项中,将z存储在[i][2]子项中。

代码语言:javascript
复制
    xyzArray[i][0] = xnew;
    xyzArray[i][1] = ynew;
    xyzArray[i][2] = znew;

要在计算完所有值后打印值,请执行以下操作:

代码语言:javascript
复制
} // end of Euler method loop

for (int i = 0; i < xyzArray.length; i++) {
    System.out.println(xyzArray[i][0] + ", " + xyzArray[i][1] +", " + xyzArray[i][2]);
}

你的revision 3 code起作用了。

使用Butterfly.main(new String[] {})

  • 查看修订版3的代码
  • 复制程序的文本并将其粘贴到jshell
  • Execute中

你会看到5000x,y,z的值--都是不同的。

代码语言:javascript
复制
C:\>"\Program Files\Java\jdk-10\bin\jshell.exe"
|  Welcome to JShell -- Version 10
|  For an introduction type: /help intro

jshell> public class Butterfly {
   ...>
   ...> public static double dx(double x, double y, double z) {
   ...>     return -10*(x - y);

         [... many lines omitted for brevity ...]

   ...>     }
   ...>    }
   ...> }
|  created class Butterfly

jshell> Butterfly.main(new String[] {})
0.2, 19.98, 24.933333333333334
0.39780000000000004, 19.960633333333334, 24.870840444444443
0.5934283333333333, 19.9419174796712, 24.81245854319926
0.786913224796712, 19.92386713960567, 24.758126085937494

         [... many lines omitted for brevity ...]

0.381817425662861, 0.5879585365342771, 12.654916756967012
0.38387883677157514, 0.59322959817818, 12.621394805096582
0.3859723443856412, 0.5985398896533907, 12.587965480571079
0.3880980198383187, 0.6038899688589537, 12.554628592467306

jshell>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50630353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档