-1

I filled an array with the numbers 1 to 10 using a for-loop. Now I need to fill a second array with the values of the first array, but upside down. So the second array has to be filled with 10 to 1.

I tried something but it fills the second array with just 1.

output is:

1
2
3
4
5
6
7
8
9
10
1
1
1
1
1
1
1
1
1
1

and it should be 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

package FillandTurn;

import java.util.Scanner;

/**
 *
 * @author Quinten
 */
public class FillandTurn {

    /**
     * @param args the command line arguments
     */
    Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        // TODO code application logic here
        FillandTurn fill = new FillandTurn();
        fill.start();
    }

    public void start(){
        FillandTurn turn = FillandTurn();
        int[] array = turn.vullen();
        int[] array2 = turn.draaien(array);
    }
    public int[] fill(){
        int[] array = new int[10];
        int j = 0;
        for(int i = 0; i < array.length; i++ ){
            j++;
            array[i] = j;
        }


        for(int p = 0; p <= array.length-1; p++ ){
            System.out.println(array[p]);
        }

        return array;
    }
    public int[] turn(int[] array){
        int[] array2 = new int[10];
        for(int x = 0; x < array2.length; x++){
            for(int y = array.length-1; y >=0; y--){
                array2[x] = array[y];
            }
        }


        for(int p = 0; p <= array2.length-1; p++ ){
            System.out.println(array2[p]);
        }


       return array2;


    }

}

Any helps is appreciated!

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67

3 Answers3

0

Have a for loop to fill the first array as stated, but then have a seperate for loop for the second array, which starts at the size of the first array, and decrements on each iteration, putting the elements into the new array

kxdan
  • 187
  • 1
  • 15
0

Have you tried reading the array from the back to the front?

public int[] reverseArray(int[] array1) {
    int[] array2 = new int[array1.length];

    int pos = 0;

    for(int neg = array1.length; neg >= 0; neg--) {
        array2[pos] = array1[neg];
    }

    return array2;
}

This is a simple way, there is probably better ways to do this.

Tomaltach
  • 913
  • 1
  • 11
  • 30
0

When you copy an array you only need one loop. Using a nested loop means you are copying each value N times, or in fact the first value overwrites all the others.

You can do

class FillAndTurn {
    public static void main(String... args) {
        int[] array = new int[10];
        fill(array);
        int[] array2 = turn(array);
        IntStream.of(array).forEach(System.out::println);
        IntStream.of(array2).forEach(System.out::println);
    }

    static void fill(int[] array) {
        for (int i = 0; i < array.length; i++)
            array[i] = i + 1;
    }

    static int[] turn(int[] array) {
        int[] array2 = new int[array.length];
        for(int i = 0; i < array.length; i++)
            array2[array2.length - i - 1] = array[i];
        return array2;
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130