0

I'm a rookie programmer, and I want to swap every element from this array one step to left, but when I write this code, there is no result, where is the wrong?

    char[] S = {'h', 'R', 'i', 'y' , 'a' ,'d'};
    char temp;
    int j =1;
    for(int i = 0 ; i< S.length ; i++){
        temp = S[i];
        S[i]=S[j];
        S[j] = temp;
        j++;
    }
    System.out.println(S[0]);
    System.out.println(S[1]);
    System.out.println(S[2]);
    System.out.println(S[3]);
    System.out.println(S[4]);
    System.out.println(S[5]); 
josejuan
  • 9,338
  • 24
  • 31
Nawaf.112
  • 13
  • 5
  • *"there is no result"* ... this is pretty unlikely. – Tom Aug 07 '21 at 19:29
  • What do you expect? I get `Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6` with your code. When `j` is `6`. `i < S.length - 1` resolves that. And output Riyadh. I repeat, what do you expect? – Elliott Frisch Aug 07 '21 at 19:32
  • Why you are closing the question? The question is clear "how to rotate left", he has simply forgotten the last element. Have a little patience with the trainees. – josejuan Aug 07 '21 at 19:34
  • 1
    @josejuan "how to rotate left" is why OP wants to do, but "he has simply forgotten the last element" is not what OP reports as the issue and no "there is no result" doesn't match that. It also doesn't the actual result when running that code. – Tom Aug 07 '21 at 19:43
  • @Tom "I want to swap every element from this array one step to left" is clearly and without any doubt an array rotation. I agree that he could write it better, but that's what editing and helping people is for. – josejuan Aug 07 '21 at 19:46
  • @josejuan *"[..] is clearly and without any doubt an array rotation"* .. correct and that is their task that they want to solve, but not the issue where they are hanging right now. – Tom Aug 07 '21 at 19:56

2 Answers2

0

you will have an exception ArrayIndexOutOfBoundsException when i = 5, j will be 6 and the length of your array is 6, you should use a condition that wont throw this exception someting like :

i < S.length - 1

 public static void main(String[] args) {
     char[] S = {
         'h',
         'R',
         'i',
         'y',
         'a',
         'd'
     };
     char temp;
     int j = 1;
     for (int i = 0; i < S.length - 1; i++) {
         temp = S[i];
         S[i] = S[j];
         S[j] = temp;
         j++;
     }
     System.out.println(S[0]);
     System.out.println(S[1]);
     System.out.println(S[2]);
     System.out.println(S[3]);
     System.out.println(S[4]);
     System.out.println(S[5]);
 }
DEV
  • 1,607
  • 6
  • 19
0

You miss the last element.

static void rotateArray(char [] S) {

    // if is empty nothing to do
    if(S.length < 1)
        return;

    // we save the first element to put at the last position
    char first = S[0];

    // for each element but last
    for(int i = 0; i < S.length - 1; i++)
        S[i] = S[i + 1];

    // now the saved first value is used
    S[S.length - 1] = first;

}

public static void main(String... args) {

    char [] helloWorld = "Hello World!".toCharArray();

    System.out.println(helloWorld);
    for(int i = 0; i < 10; i++) {
        rotateArray(helloWorld);
        System.out.println(helloWorld);
    }

}

with output

Hello World!
ello World!H
llo World!He
lo World!Hel
o World!Hell
 World!Hello
World!Hello 
orld!Hello W
rld!Hello Wo
ld!Hello Wor
d!Hello Worl
josejuan
  • 9,338
  • 24
  • 31