-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStringBuffer.java
More file actions
63 lines (56 loc) · 1.6 KB
/
Copy pathMyStringBuffer.java
File metadata and controls
63 lines (56 loc) · 1.6 KB
1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package labpad;
import java.util.Arrays;
/**
*
* @author epicur
*/
public class MyStringBuffer {
char[] str;
int limit;
final int CHUNK = 10;
MyStringBuffer(){
str = new char[CHUNK];
//limit = =;
}
MyStringBuffer insert(int offset, char ch){
if (offset > limit || offset < 0)
throw new IndexOutOfBoundsException();
if (limit == str.length){
char[] tmp = str;
//str = new char[str.length * 2];
//System.arraycopy(tmp, 0 , str, 0, tmp.length);
str = Arrays.copyOf(tmp, str.length * 2);
}
for (int i = limit; i > offset; i--)
str[i] = str[i - 1];
str[offset] = ch;
limit++;
return this;
}
MyStringBuffer deleteCharAt (int offset){
for (int i = offset; i < limit -1; i++)
str[i] = str[i+1];
limit--;
return this;
}
void setChar(int offset, char ch){
if (offset > limit || offset < 0)
throw new IndexOutOfBoundsException();
if (limit == str.length){
char[] tmp = str;
//str = new char[str.length * 2];
//System.arraycopy(tmp, 0 , str, 0, tmp.length);
str = Arrays.copyOf(tmp, str.length * 2);
}
str[offset] = ch;
limit++;
}
public String toString(){
return new String(str, 0, limit);
}
}