如何访问循环内外的整个双数组

时间:2015-03-30 10:18:14

标签: java arrays for-loop

我正在编写一个程序,找到方程y = a + b + c的最佳答案(实际上这个方程会更难,但这里只是简短的例子)。 问题是我的排序代码无法在循环中访问整个数组。 有没有办法将整个数组传递给排序代码?

package pkg03;

public class Main {

public static void main(String[] args) {

    double a1 = 0;
    double a2 = 3;
    double aZ = 3;
    double b1 = 0;
    double b2 = 3;
    double bZ = 3;
    double c1 = 0;
    double c2 = 3;
    double cZ = 3;

    double aT = (a2 - a1) / aZ;
    double bT = (b2 - b1) / bZ;
    double cT = (c2 - c1) / cZ;

    double a11 = a1 - aT;
    double b11 = b1 - bT;
    double c11 = c1 - cT;
    ciklas(a11, a2, aT, aZ, b11, b2, bT, bZ, c11, c2, cT, cZ);

}
public static void ciklas(double aS, double aE, double aD, double aZS, double bS, double bE, double bD, double bZS, double cS, double cE, double cD, double cZS){
    double z1;
    double a = aS;
    for (z1=0; z1<=aZS; z1++) {

        a = a + aD;
        double z2;
        double b = bS;
        for (z2=0; z2<=bZS; z2++){

            b = b + bD;
            double z3;
            double c = cS;
            for (z3=0; z3<=cZS; z3++){
                c = c + cD;
                double y = a + b + c;

                double[][] masyvas ={
                    {y},
                    {a, b, c}};

//问题在这里!排序代码只能获得一行数组。所以它无法比较这些值。

    double temp;
    double temp2;
    double temp3;
    double temp4;

    boolean fixed=false;
    while(fixed==false){
        fixed=true;
        for(int i=0; i<masyvas[0].length-1; i++){
            if(masyvas[0][i] > masyvas[0][i+1]){
                temp = masyvas[0][i+1];
                temp2 = masyvas[1][i+1];
                temp3 = masyvas[1][i+masyvas[0].length+1];
                temp4 = masyvas[1][i+(masyvas[0].length)*2+1];


                masyvas[0][i+1] = masyvas[0][i];
                masyvas[1][i+1] = masyvas[1][i];
                masyvas[1][i+masyvas[0].length+1] = masyvas[1][i+masyvas[0].length];
                masyvas[1][i+(masyvas[0].length*2)+1] = masyvas[1][i+(masyvas[0].length*2)];

                masyvas[0][i] = temp;
                masyvas[1][i] = temp2;
                masyvas[1][i+masyvas[0].length] = temp3;
                masyvas[1][i+(masyvas[0].length*2)] = temp4;


                fixed=false;
            }
        }
    }


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

}



            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

没有理由不能简单地将数组作为参数传递:

public void fun (double[][] array){}

或者,更确切地说,对该数组的。这就是它在Java中的工作方式。