简体   繁体   中英

Why is my array showing 0s when I flip it and reverse it?

Trying to reverse an user-input array.

My code

    Scanner s = new Scanner(System.in);
    System.out.println("Enter numbers: ");
    int[] array = new int[5];
    int[] reversed = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        array[i] = s.nextInt();
        reversed[i] = array[array.length - 1 - i];
    }
    System.out.println(Arrays.toString(reversed));

When I enter:

 10
 20
 30
 40
 50

I get:

 output:
 [0, 0, 30, 20, 10]

Yet when I hardcode numbers, it works just fine.

  int[] array = {10, 20, 30, 40, 50};
    int[] reversed = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        reversed[i] = array[array.length - 1 - i];
    }
    System.out.println(Arrays.toString(reversed));

I get back:

 [50, 40, 30, 20, 10]

Not sure what I'm doing wrong here. I know there are other ways to do the same thing but I'm trying to figure out why the way I'm doing it is giving me issues.

You are filling your arrays from beginning to end, and you are filling reversed with values from array , before they were populated!

Specifically, once you try to fill reversed[0] , you use array[4] , which was not populated yet.

You can overcome it, by first filling array , and start filling reversed in a second loop after the first is complete.

just change this line:

 reversed[i] = array[array.length - 1 - i];

to:

 reversed[array.length-1-i] = array[i];

as noted you are trying to access array before it is filled

Try the below code to implement the reversal of the array:

import java.util.Scanner;
import java.util.Arrays;


public class Main {
    
    public static void main(String args[]) { 
        
        Scanner s = new Scanner(System.in);
        System.out.println("Enter numbers: ");
        int[] array = new int[5];
        int[] reversed = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = s.nextInt();
        }

        int j = array.length; 
        for (int i = 0; i < array.length; i++) { 
            reversed[j - 1] = array[i]; 
            j = j - 1; 
        } 

        System.out.println("Reversed array is: \n"); 
        for (int k = 0; k < array.length; k++) { 
            System.out.println(reversed[k]); 
        } 
    } 
}

Output:

Enter numbers:                                                                                                                                                
1                                                                                                                                                             
2                                                                                                                                                             
3                                                                                                                                                             
4                                                                                                                                                             
5                                                                                                                                                             
Reversed array is:

5                                                                                                                                                             
4                                                                                                                                                             
3                                                                                                                                                             
2                                                                                                                                                             
1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM