-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynArray.java
80 lines (71 loc) · 1.99 KB
/
DynArray.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class DynArray<ContentType> {
private ContentType[] liste; // Liste
private int aktPos; // Index des letzten Elements der Liste
public DynArray(){
liste =(ContentType[]) new Object[0];
aktPos=0;
}
public void append(ContentType inhalt){
if (isEmpty()) {
ContentType[] hilfsarray=(ContentType[]) new Object[1];
hilfsarray[aktPos]=inhalt;
liste=hilfsarray;
} else {
ContentType [] hilfsarray =(ContentType[]) new Object[liste.length+1];
for (int i=0;i<=aktPos;i++){
hilfsarray[i]=liste[i];
}
hilfsarray[aktPos+1] = inhalt;
liste = hilfsarray;
aktPos++;
}
}
public void insertAt(int index, ContentType inhalt){
if ((index>=0)&&(index <=aktPos)){
ContentType [] hilfsarray =(ContentType[]) new Object[liste.length+1];
for (int i=0;i<index;i++){
hilfsarray[i]=liste[i];
}
hilfsarray[index] = inhalt;
for (int i=index;i<=aktPos;i++){
hilfsarray[i+1]=liste[i];
}
liste = hilfsarray;
aktPos++;
}
else if (index == liste.length){
append(inhalt);
} // end of if-else
}
public ContentType getItem(int index){
if (!isEmpty()){
return liste[index];
}
else return null;
}
public void setItem(int index, ContentType inhalt){
liste[index]=inhalt;
}
public void delete(int index){
if (!isEmpty()&& index<=aktPos){
ContentType[] hilfsarray = (ContentType[]) new Object[liste.length-1];
for (int i=0;i<index;i++){
hilfsarray[i]=liste[i];
}
for (int i=index+1;i<liste.length;i++){
hilfsarray[i-1]=liste[i];
}
aktPos--;
liste = hilfsarray;
if(aktPos < 0){
aktPos = 0;
}
}
}
public boolean isEmpty(){
return (liste.length==0);
}
public int getLength(){
return liste.length;
}
}