Skip to main content
Modulo
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

Simplicity

To rotate a string, only the string itself and the offset of rotation are needed.

A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.

In Java not needing a class is signalled by the use of static methods.

Also, using String.substring you can avoid the manual looping. In short all your program can be reduced to:

public static String rotate(String s, int offset) {
  int i = offset % s.length();
  return s.substring(offseti) + s.substring(0, offseti);
}

The example usage is also simplified, as you do not need to build a new class:

public static void main(final String... args) {
  for (int i = 0; i < 6; i++) {
    System.out.println(rotate("abcdefg", i));
  }
}

The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.

Simplicity

To rotate a string, only the string itself and the offset of rotation are needed.

A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.

In Java not needing a class is signalled by the use of static methods.

Also, using String.substring you can avoid the manual looping. In short all your program can be reduced to:

public static String rotate(String s, int offset) {
  return s.substring(offset) + s.substring(0, offset);
}

The example usage is also simplified, as you do not need to build a new class:

public static void main(final String... args) {
  for (int i = 0; i < 6; i++) {
    System.out.println(rotate("abcdefg", i));
  }
}

The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.

Simplicity

To rotate a string, only the string itself and the offset of rotation are needed.

A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.

In Java not needing a class is signalled by the use of static methods.

Also, using String.substring you can avoid the manual looping. In short all your program can be reduced to:

public static String rotate(String s, int offset) {
  int i = offset % s.length();
  return s.substring(i) + s.substring(0, i);
}

The example usage is also simplified, as you do not need to build a new class:

public static void main(final String... args) {
  for (int i = 0; i < 6; i++) {
    System.out.println(rotate("abcdefg", i));
  }
}

The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.

Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

Simplicity

To rotate a string, only the string itself and the offset of rotation are needed.

A class is a way to store data in an orderly manner, but for the task of rotating a string, you do not need such storage.

In Java not needing a class is signalled by the use of static methods.

Also, using String.substring you can avoid the manual looping. In short all your program can be reduced to:

public static String rotate(String s, int offset) {
  return s.substring(offset) + s.substring(0, offset);
}

The example usage is also simplified, as you do not need to build a new class:

public static void main(final String... args) {
  for (int i = 0; i < 6; i++) {
    System.out.println(rotate("abcdefg", i));
  }
}

The drawback of this approach is that it may be slower and/or use more memory, but unless you want to rotate a string tens of millions of times per second it should not matter.