Compact strings with AsciiCharSequence
Interface
# Problem
Compact strings with AsciiCharSequence
https://hyperskill.org/learn/step/3082
注意
Wow! This problem is kind of tricky. If you're ready to put your thinking cap on, brace yourself and good luck! Otherwise, you can skip it for now and return any time later
Strings in Java implement java.lang.CharSequence
interface. Since Java internally uses UTF-16, 2 bytes are required to store each char. At the same time, ASCII encoding allows storing character codes in one byte and includes all Latin letters, digits, and standard special characters. Compared to the standard String class, ASCII-character sequences require half the memory.
Write a class named AsciiCharSequence
for storing ASCII-character sequences, that should:
- implement the interface
java.lang.CharSequence
; - have a constructor that takes a byte array;
- have methods
int length()
,char charAt(int idx)
,CharSequence subSequence(int from, int to)
, andString toString()
.
You can find the declaration of methods and their behavior in the description of java.lang.CharSequence
(JavaDoc (opens new window) or sources).
Carefully check signatures of abstract methods of java.lang.CharSequence
interface, especially subSequence
method. It accepts 2 integers: start index (inclusive) and end index (exclusive). The method returns an object of a class that implements java.lang.CharSequence
interface. In this example it will be an instance of AsciiCharSequence
class.
Note: the testing system will always pass correct input parameters to overridden methods.
P.S. This feature is supported since Java 9 in standard strings. For details, see this article on compact strings in Java 9 (opens new window).
# Hint & Explain
// 输入提示
# Solution
# Other solution 1
import java.util.*;
class AsciiCharSequence implements CharSequence {
private final byte[] bytes;
public AsciiCharSequence(byte[] bytes) {
this.bytes = bytes.clone();
}
@Override
public int length() {
return this.bytes.length;
}
@Override
public char charAt(int idx) {
return (char) this.bytes[idx];
}
@Override
public CharSequence subSequence(int from, int to) {
byte[] sequence = new byte[to - from];
for (int i = from; i < to; i++) {
sequence[i - from] = this.bytes[i];
}
return new AsciiCharSequence(sequence);
}
@Override
public String toString() {
return new String(this.bytes, StandardCharsets.UTF_8);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Other solution 2
import java.util.*;
class AsciiCharSequence implements CharSequence {
private final byte[] charSequence;
public AsciiCharSequence(byte[] charSequence) {
this.charSequence = charSequence.clone();
}
@Override
public int length() {
return charSequence.length;
}
@Override
public char charAt(int index) {
return (char) charSequence[index];
}
@Override
public CharSequence subSequence(int start, int end) {
return new AsciiCharSequence(Arrays.copyOfRange(charSequence, start, end));
}
@Override
public String toString() {
return new String(charSequence);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29