Home > OS >  Rotate and return the ArrayList<Integer> in java
Rotate and return the ArrayList<Integer> in java

Time:10-30

here in below program i am defining ArrayList of size 7 [1,2,3,4,5,6,7] and and rotating right by 3 places but when i print the numbers1 list it gives me my expected answer numbers: [5, 6, 7, 1, 2, 3, 4] but when i return this ArrayList and then print it, it gives me this answer numbers: [2, 3, 4, 5, 6, 7, 1] why is that please explain.

package QAIntvSprint;

import java.util.ArrayList;
import java.util.Scanner;

public class RotateArray {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int k = sc.nextInt();

    ArrayList<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < n; i  ) {
      numbers.add(sc.nextInt());
    }
    rotateArray(numbers, k);
    ArrayList<Integer> ans = rotateArray(numbers, k);
    for (Integer x : ans) {
      System.out.print(x   " ");
    }
  }

  static ArrayList<Integer> rotateArray(ArrayList<Integer> numbers, int k) {
    for (int i = 0; i < k; i  ) {
      numbers.add(0, numbers.get(numbers.size() - 1));
      numbers.remove(numbers.size() - 1);
    }
    ArrayList<Integer> numbers1 = numbers;
    System.out.println("numbers: "   numbers1.toString());
    return numbers1;
  }
}

output

enter image description here

CodePudding user response:

You are running your function twice:

rotateArray(numbers, k); // first
ArrayList<Integer> ans = rotateArray(numbers, k); // second

So the numbers are shifted by 6 instead of 3 positions

Besides, your function does not create a new copy of the array
You return a reference to the same array

ArrayList<Integer> numbers1 = numbers; // copies a reference to an object, not the object itself
return numbers1; // returns the original link

So i suggest refactoring your code like this:

public class RotateArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        
        ArrayList<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < n; i  ) {
            numbers.add(sc.nextInt());
        }
        
        // the function will modify the ArrayList by reference, it will change
        rotateArray(numbers, k);
        for (Integer x : numbers) {
            System.out.print(x   " ");
        }
    }
    
    static void rotateArray(ArrayList<Integer> numbers, int k) {
        for (int i = 0; i < k; i  ) {
            numbers.add(0, numbers.get(numbers.size() - 1));
            numbers.remove(numbers.size() - 1);
        }
    }
}

CodePudding user response:

That's because your function rotateArray changes your numbers ArrayList you give it as parameter. So basically it changes the List (numbers) in your main method. And because you call the method two times, the array gets rotated in sum 6 times (like you see in your final output)

If you don't want to change the ArrayList you give in as a parameter, you should do something like

ArrayList<Integer> newList = new ArrayList(numbers);

as first in you rotateArray method and then rotate this list.

CodePudding user response:

here ArrayList<Integer> ans = rotateArray(numbers, k); rotates numbers, returned by the previous line

ArrayList in java is passed by reference just delete this line

rotateArray(numbers, k);

  • Related