0

So I got a little question again about array swapping.

So I got this little piece of code here:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Random random = new Random();
    int indexe = scan.nextInt();
    System.out.println("Ihre Indexauswahl: " + indexe);
    int[] array = new int[indexe];
    System.out.println("Array:");
    System.out.println("{");
    for (int i = 0; i < array.length; i++) {
        array[i] = 1 + random.nextInt(99);
        System.out.println(i + " => " + array[i]);
    }

    System.out.println("}\nArray:\n{");
    for (int i = 0; i < array.length / 2; i++) {
        int temp = array[i];
        array[i] = array[array.length - i - 1];
        array[array.length - i - 1] = temp;
        System.out.println(i + " => " + array[i]);
    }
    System.out.println("}");
}

I tested the swapping from my array and If I have 10 indexes in my array, and swap it, then only 5 indexes show up in the System.out.print.

If I remove the " /2 " in the for condition then I get 10 indexes but wrong values in the last 5.

How can I fix this?

Picture with the "/2":

Picture with the "/2"

Picture without the "/2":

enter image description here

Ayush Mishra
  • 267
  • 3
  • 14
Marius1773
  • 23
  • 5
  • 3
    You should print **after** swapping, not during swapping. – Mark Rotteveel May 11 '20 at 07:54
  • There is nothing wrong with swapping. The array has been reversed correctly. The problem is with printing. You should move the printing out of the loop where you are swapping the elements. Either you can print the reversed array with just one statement, `System.out.println(Arrays.toString(array));` or you can do it using a loop e.g. `for(int i=0;i"+array[i]);}` – Arvind Kumar Avinash May 11 '20 at 08:18

3 Answers3

0

Print after swapping every element

public static void main(String[] args) {
    Random random = new Random();
    int[] array = new int[10];

    for (int i = 0; i < array.length; i++) {
        array[i] = 1 + random.nextInt(99);
    }
    printArray(array);
    for (int i = 0; i < array.length / 2; i++) {
        int temp = array[i];
        array[i] = array[array.length - i - 1];
        array[array.length - i - 1] = temp;

    }
    printArray(array);
}

private static void printArray(int[] array) {
    System.out.println("Array: {");
    for (int i = 0; i < array.length; i++) {
        System.out.println(i + " => " + array[i]);
    }
    System.out.println("}");
}
sidgate
  • 14,650
  • 11
  • 68
  • 119
0

So as per your logic you are taking the index from last and index from beginning iterating over the array, so basically you should print the output after the swapping because, swapping is done only array.length / 2 times, hence its printing only half of the time.

try executing the below code:

public static void main(String []args){
        Scanner scan = new Scanner(System.in);
        Random random = new Random();
        int indexe = scan.nextInt();
        System.out.println("Ihre Indexauswahl: " + indexe);
        int[] array = new int[indexe];
        System.out.println("Array:");
        System.out.println("{");
        for (int i = 0; i < array.length; i++) {
            array[i] = 1 + random.nextInt(99);
            System.out.println(i + " => " + array[i]);
        }

        for (int i = 0; i < array.length / 2; i++) {
            int temp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i - 1] = temp;
        }
        System.out.println("}\nArray:\n{");
        for(int i = 0;  i < array.length ; i++) {
            System.out.println(i + " => " + array[i]);
        }
        System.out.println("}");

     } 
Ayush Mishra
  • 267
  • 3
  • 14
0

well i have a simple logic: if you wanna use an array say h[]

take two variables a,b. a is the start point,b is the end. so b=length of array.

now run a loop until you get to the midpoint of the array,because you will be swapping the numbers until you reach the midpoint only.

so swap the the first element with the last and so on until you reach the midpoint

int x=a[i]; a[i]=b[i]; x=b[i]; i is iterating through the array h

yoron
  • 1
  • 4