Home > Net >  How to correct the order of my attempted reverse array?
How to correct the order of my attempted reverse array?

Time:02-14

I was working on a project that would see an array of decimals be printed out in reverse and in a lateral printing. I have gotten the java code to be printed in lateral format, but when it comes to have my arrays variables printed, it is not in revers order, but in a random order. I am not sure where I have gone wrong, looking for a second pare of eyes to catch where I went wrong, thanks.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    DecimalFormat decimalFormat = new DecimalFormat("#.##");
    double [] dArray = {3.1415, 17.4567, 8.3132};
    int i = 0;
    for (double d : dArray) {
        dArray [i] = Double.valueOf(decimalFormat.format(d));
        i  ;
    }
    for (double d = dArray.length-1;d>=1;d--)
        //System.out.println(dArray[i]);    
    Arrays.stream(dArray).forEach(System.out::println);
}



}

CodePudding user response:

You made a few mistakes in your code:

In

for (double d = dArray.length-1;d>=1;d--)
    System.out.println(dArray[i]);

d should be an int and should be decremented down to zero and not one. Also, dArray[i], should've been dArray[d] since that is what you chose as counter variable. Lastly, the statement System.out.println(dArray[i]); won't print out the internal values of the array. It will print out the object's hashcode (yes, arrays are objects in Java).

for (double d = dArray.length-1;d>=1;d--)
    //System.out.println(dArray[i]);    
    Arrays.stream(dArray).forEach(System.out::println);

Because you fail to use curly braces with your loop, commenting out your previous attempt to print out the array produced the ill effect of calling the stream operation INSIDE THE LOOP. So basically, you are printing out the whole array while iterating through it. The contents of the array is then printed twice. You should've commented out the for loop as well.

The solution:

public class ReverseArray {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        double[] dArray = {3.1415, 17.4567, 8.3132};
        double[] reversedArray = new double[dArray.length];
        System.out.print("[");
        for(int i = 0; i < dArray.length; i  ) {
            double value = dArray[i];
            System.out.print(value);
            if (i < dArray.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");

        for (int i = dArray.length - 1, j = 0; i >= 0; i--, j  ) {
            reversedArray[j] = dArray [i];
        }
        System.out.print("[");
        for(int i = 0; i < reversedArray.length; i  ) {
            double value = reversedArray[i];
            System.out.print(value);
            if (i < reversedArray.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
    }
}

The above code outputs:

[3.1415, 17.4567, 8.3132]
[8.3132, 17.4567, 3.1415]

You can use DecimalFormat to display the output using your pattern. You should not use it to format the values and then save them in your array. If you were doing that in financial software, you would end up in jail eventually. Using your decimal format pattern, I can modify the two System.out.print() as follows: System.out.print(decimalFormat.format(value));. With that change, the program now outputs:

[3.14, 17.46, 8.31]
[8.31, 17.46, 3.14]

Without modifying the values of the array.

CodePudding user response:

When you use stream in forEach(), the order is not strictly maintained. Instead forEachOrdered() should be used. Like,

Arrays.stream(dArray).forEachOrdered(System.out::println);
  • Related