0

i've got code that changes lower case latters to upper case and the other way around. What is the faster way of doing it that what i wrote?

public static String flipcase(String text) {

   ArrayList<Character> arry2 = new ArrayList<Character>();
   char[] array = text.toCharArray();
   char x = ' ';
   char y = ' ';

   for (int i = 0; i <array.length; i++) {
       if (Character.isLowerCase(array[i])) {
           x = Character.toUpperCase(array[i]);
           arry2.add(x);
       } else if (Character.isUpperCase(array[i])){
           y =  Character.toLowerCase(array[i]);
           arry2.add(y);
       } else if (Character.isSpace(array[i])) {
           arry2.add(array[i]);
       }
   }
   StringBuilder result = new StringBuilder(arry2.size());
   for (Character c : arry2) {
       result.append(c);
   }
   String output = result.toString();

   return output;
}

public static void main(String[] args) {
   System.out.println(flipcase("To jest Ten FLIP Case"));
}
  • 3
    Just manipulate directly in `array`, and use `new String(array)` at the end. – Andy Turner May 10 '17 at 20:21
  • @RobinTopper there's a subtle little difference in this question, which is that some characters are discarded from the input. It doesn't change it *that* much though. – Andy Turner May 10 '17 at 20:29
  • @AndyTurner that may just be an unintentional feature of this particular implementation. OP didn't specify that he wanted to discard characters, and it may be that he just didn't think about what should happen when there are numbers or punctuation characters in the input. – Dawood ibn Kareem May 10 '17 at 20:36
  • @DawoodibnKareem OP specifically asked "What is the faster way of doing it that what i wrote?", so the behavior (intentional or not) should be preserved. – Andy Turner May 10 '17 at 20:37
  • @AndyTurner, you're right, of course, and I take back my comment. Of course, it really depends on whether he meant "what is a faster way of doing what I wrote" or "what is a faster way of doing it than what I wrote", but that's a bit hair-splittish. I still think RobinTopper was right to identify the duplicate, and it's a shame that his/her comment has now gone. – Dawood ibn Kareem May 10 '17 at 20:41

1 Answers1

2

There are two reasons why I'd say your code will be slow:

  1. You're putting the changed letters into a List: this necessarily means that you are boxing the chars to Characters, which takes time if you're doing a lot of them.
  2. You're putting the changed letters into a list, and then copying them into a StringBuilder. You could simply put them straight into the StringBuilder.

But I'd say it is faster just to manipulate directly in array, and use new String(array) at the end.

char[] array = text.toCharArray();
int j = 0;
for (int i = 0; i <array.length; i++) {
   if (Character.isLowerCase(array[i])) {
       array[j++] = Character.toUpperCase(array[i]);
   } else if (Character.isUpperCase(array[i])) {
       array[j++] = Character.toLowerCase(array[i]);
   } else if (Character.isSpace(array[i])) {
       array[j++] = array[i];
   }
}
return new String(array, 0, j);

toCharArray() returns a copy of the char array backing the string, so you're free to modify it.

This is basically what StringBuilder is doing; it just does it with one fewer "layer".

Andy Turner
  • 137,514
  • 11
  • 162
  • 243