如何将数组的每一列分配给特定变量?

时间:2015-01-13 20:17:20

标签: java

这是将数据文件中的每一列分配给特定变量的正确方法吗?

public static void main(String[] args) {
    //specifying the path to file
    String datafile = " C:\\Users\\rez\\Desktop\\sol_2.mcmc";
    //reading the file
    double[][] mydata = FileReadingTools.getDoubleArray(datafile);

    double P_0; //days
    double M_0; // in days
    double e_0;
    double w_0 = Math.toRadians(0);

    double[][] list = new double[3000][50];
    for (int sol = 0; sol < 3000; sol++) {

        list[sol][0] = P_0;
        list[sol][2] = M_0;
        list[sol][3] = e_0;
        list[sol][4] = w_0;

        System.out.println(P_0 + " " + M_0);
    }

1 个答案:

答案 0 :(得分:1)

我相信你已经用你的变量赋值交换了左右两边。您想要从数组中分配值。另外,请使用更具描述性的变量名称。我想你想要的东西,

for (int sol = 0; sol < mydata.length; sol++) {
    P_0 = mydata[sol][0]; // mydata v-- as noted in the comments. ---v
    M_0 = mydata[sol][2];
    e_0 = mydata[sol][3];
    w_0 = mydata[sol][4];

或者,您可以使用printf并使用类似

的内容直接访问数组
System.out.printf("%.2f %.2f", mydata[sol][0], mydata[sol][2]);