Kaynağa Gözat

Use StringBuilder in methods

Jack Mustacato 8 yıl önce
ebeveyn
işleme
f578ab64a1

+ 9
- 9
exercises/atbash-cipher/.meta/src/reference/java/Atbash.java Dosyayı Görüntüle

9
 
9
 
10
     String encode(String input) {
10
     String encode(String input) {
11
         String encoded = stripInvalidCharacters(input).toLowerCase();
11
         String encoded = stripInvalidCharacters(input).toLowerCase();
12
-        String cyphered = "";
12
+        StringBuilder cyphered = new StringBuilder(input.length());
13
 
13
 
14
         for (char c : encoded.toCharArray()) {
14
         for (char c : encoded.toCharArray()) {
15
-            cyphered += applyCipher(c);
15
+            cyphered.append(applyCipher(c));
16
         }
16
         }
17
 
17
 
18
-        return splitIntoFiveLetterWords(cyphered);
18
+        return splitIntoFiveLetterWords(cyphered.toString());
19
     }
19
     }
20
 
20
 
21
     String decode(String input) {
21
     String decode(String input) {
22
         String encoded = stripInvalidCharacters(input).toLowerCase();
22
         String encoded = stripInvalidCharacters(input).toLowerCase();
23
-        String deciphered = "";
23
+        StringBuilder deciphered = new StringBuilder(input.length());
24
 
24
 
25
         for (char c : encoded.toCharArray()) {
25
         for (char c : encoded.toCharArray()) {
26
-            deciphered += applyCipher(c);
26
+            deciphered.append(applyCipher(c));
27
         }
27
         }
28
 
28
 
29
-        return deciphered;
29
+        return deciphered.toString();
30
     }
30
     }
31
 
31
 
32
     String stripInvalidCharacters(String input) {
32
     String stripInvalidCharacters(String input) {
33
-        String filteredValue = "";
33
+        StringBuilder filteredValue = new StringBuilder(input.length());
34
 
34
 
35
         for (char c : input.toCharArray()) {
35
         for (char c : input.toCharArray()) {
36
             if (Character.isLetterOrDigit(c)) {
36
             if (Character.isLetterOrDigit(c)) {
37
-                filteredValue += c;
37
+                filteredValue.append(c);
38
             }
38
             }
39
         }
39
         }
40
 
40
 
41
-        return filteredValue;
41
+        return filteredValue.toString();
42
     }
42
     }
43
 
43
 
44
     private char applyCipher(char input) {
44
     private char applyCipher(char input) {