Home > Net >  How to display 3 arrays using the format: element element = element in Java?
How to display 3 arrays using the format: element element = element in Java?

Time:04-03

Like the title suggests, I am trying to display elements from multiple arrays in the format above. I needed to create 3 arrays with a length of 25. The first is filled using the loop counter, the second is the loop counter squared and the 3rd array is filled with the sum of array 1 and 2. I have all three arrays filled but when I compile and run it, it seems to output what I want but loops way more times then I need it to.

public static void main(String args[]) throws Exception {
    
    double[] array1 = new double[25];
    double[] array2 = new double[25];
    double[] array3 = new double[25];
    
    
    for (int i = 0; i < array1.length; i  ) {
        array1[i] = i   1.0;
        for (int j = 0; j < array2.length; j  ) {
            array2[j] = array1[i] * array1[i];
            for (int k = 0; k < array3.length; k  ) {
                array3[k] = array1[i]   array2[j];
                System.out.println(array1[i]   "   "   array2[j]   "   "   " = "   array3[k]   " ");
                
            }
        }
    }
}

CodePudding user response:

You made too many for you just need one

    public static void main(String args[]) throws Exception {
        
        double[] array1 = new double[25];
        double[] array2 = new double[25];
        double[] array3 = new double[25];
        
        
        for (int i = 0; i < array1.length; i  ) {
            array1[i] = i   1.0;
            array2[i] = array1[i] * array1[i];
            array3[i] = array1[i]   array2[i];
            System.out.println(array1[i]   "   "   array2[i]   "   "   " = "   array3[i]   " ");
        }

    }
}

CodePudding user response:

You have overcomplicated the problem, you're repeating each squaring 25 times, and each addition 625 times, you need only one loop. That can be seen by looking at each arrays at the end, their filled with the last iteration, everywhere

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0]
[625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0, 625.0]
[650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0, 650.0]

One loop is enough, one index is enough:

for (int i = 0; i < array1.length; i  ) {
    array1[i] = i   1.0;
    array2[i] = array1[i] * array1[i];
    array3[i] = array1[i]   array2[i];
    System.out.println(array1[i]   "   "   array2[i]   " = "   array3[i]);
}

Note that if you don't need the values after the loop, don't store them

for (int i = 1; i < array1.length   1; i  ) {
    System.out.println(i   "   "   (i * i)   " = "   (i   i * i));
}

CodePudding user response:

You don't need three nested loops, just a single loop over the shared length:

for (int i = 0; i < array1.length; i  ) {
    array1[i] = i   1.0;
    array2[j] = array1[i] * array1[i];
    array3[k] = array1[i]   array2[j];
    System.out.println(array1[i]   "   "   array2[j]   "   "   " = "   array3[k]   " ");
}
  • Related