-1

I want to shift elements in an array to the right by 1. The last element should go to position 0. So far, I only have the shifting to the right part done, I want to see if this is the right but when I press run, nothing shows up in the console. I know I have to put System.out.println(); in the main but I don't know what to call with the print command. Here is my code.

I tried putting in System.out.println(rotate({1,2,3,4})); into main but I get an error...

public class Rotate {
    static void rotate(int[] A) {
        int arrayLength = A.length;

        for(int i = 0; i <= arrayLength-1; i++){
            A[i] = A[i+1];
        }  
    }

    public static void main(String[] args){

    }
}
Soon Kwon
  • 25
  • 1
  • 7
  • 1
    Aside from the fact that your shifting functionality won't work, you need to invoke the method against an actual array, then print out the resulting array (either by returning the array itself, a copy, or by printing out the mutated array in the method itself (ugly). – Mena Aug 23 '16 at 14:55
  • You should put the `System.out.println(Arrays.toString(array));` right after you call `rotater.rotate(array);` Oh, wait.... – bradimus Aug 23 '16 at 14:56
  • @bradimus next question: "why does it print `[I@someHashCode`?" – Mena Aug 23 '16 at 14:59
  • Your method doesn't return an array, and you are printing it wrong anyway – OneCricketeer Aug 23 '16 at 14:59
  • The solution that someone just put up (got deleted) works somewhat. I believe my problem was (aside from the rotate method) was that I wasn't creating an array for the print method to test. correct? – Soon Kwon Aug 23 '16 at 15:10
  • @SoonKwon I'm not sure which answer was posted originally, but I posted another one that should explain it a bit further – Dylan Meeus Aug 23 '16 at 15:12

2 Answers2

1

You need to make a call to a function that can print, for example System.out.println(). Now there are some other issues with your code and you'll need to make some changes (there is more than one way to do this). One way would be to return an array from your rotate function, and then print the arrays that is returned.

static int[] rotate(int[] A) {
    int arrayLength = A.length;

    for(int i = 0; i <= arrayLength-1; i++){
        A[i] = A[i+1];
    }
    return A;
}

We can call this now from our main method, and print each element. If you just call the System.out.println()method and pass it an array, it will print the Hashcodefor this object. This is not that usefull for printing information of an array. So instead, you can write a loop and print each element in the array.

public static void main (String[] args)
{
    int[] x = {1,2,3,4};
    int[] y = (rotate(x));
    System.out.println(y) // prints the hash, not what you want..


    for(int i = 0; i < y.length(); i++){
       System.out.println(y[i]);
     }
}

Instead of printing each element of the array seperately, you could also print the entire array using System.out.println(Arrays.toString(y));.


You'll still get an error now

This is because your rotate method is not implemented correctly. But that is beyond the scope of this question I suppose.

To verify that this is actually working, you could try to print the array before applying rotateto it.

      int[] x = {1,2,3,4};
      for(int i = 0; i < x.length; i++) {
           System.out.println(x[i]);
      }

      // or alternative
      System.out.println(Arrays.toString(x));
Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49
  • Thank you for the detailed explaination. I will try to fix my rotate method and see. Thank you again so much – Soon Kwon Aug 23 '16 at 15:13
  • @SoonKwon You're welcome :-) – Dylan Meeus Aug 23 '16 at 15:15
  • so If i want to print it as an array...right now, I'm doing System.out.println(rotate(p)); in main... getting an error: The method println(boolean) in the type PrintStream is not applicable for the arguments (void) – Soon Kwon Aug 23 '16 at 15:29
  • I should have read some other posts...so far I have: rotate.System.out.println(Arrays.toString(p)); but this would only print the un-rotated array. how would I apply the rotate method to this? – Soon Kwon Aug 23 '16 at 15:40
0

To solve rotate and print problem (and if you don't want to return array):

static void rotate(int[] A) {
    int arrayLength = A.length;

    int t = A[arrayLength-1];

    for (int i = arrayLength - 1; i > 0; i--) {
        A[i] = A[i-1];
    }

    A[0] = t;

    for (int i : A) {
        System.out.println(i);
    }
}
Shahid
  • 2,288
  • 1
  • 14
  • 24