如何通过一个循环读取多个数组?

时间:2016-05-18 00:45:44

标签: java

例如,我想将数组u1与其他数组相乘?

HomeFragment homeFragment = 
      (HomeFragment)getSupportFragmentManager().getFragments().get(0);

3 个答案:

答案 0 :(得分:1)

for(int i = 0; i < u1.length; i++)
{
   u1[i] = value;
   u2[i] = value;
   //Etc
}

答案 1 :(得分:1)

这里我得到的但不完整:

public class Diff {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    double[] u1 = {4,2,3,5,3,4};
    double[] u2 = {3,0,2,4,2,3};
    double[] u3 = {5,0,4,4,4,5};
    double[] u4 = {0,1,2,4,2,3};
    double[] u5 = {0,3,4,4,4,5};
    double[] u6 = {3,1,0,4,2,3};
    double[] u7 = {2,5,3,0,3,4};
    double[] u8 = {3,1,1,4,2,3};
    double[] u9 = {3,1,2,3,2,3};
    double[][] u = {u2,u3};

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

    }

    }
}

}

答案 2 :(得分:1)

由于您的数组大小相同,因此只需一个索引就可以同时迭代所有数组,因此,在一个简单的for cicle中,您可以一次访问和操作所有数组中的数据。在你的情况下,如果你想乘以u1和u2:

   double multiply[] = new double[u1.length];
   for(int i = 0; i < u1.length; i++){
        multiply[i] = u1[i] * u2[i];
   }