Skip to content

Commit 392ad65

Browse files
committed
lesson #7
1 parent 67a39d0 commit 392ad65

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package lesson7;
2+
3+
import java.util.List;
4+
import java.util.Stack;
5+
import java.util.Vector;
6+
7+
public class Lesson7 {
8+
public static void main(String[] args) {
9+
SimpleBox<Integer> box1 = new SimpleBox<>(5);
10+
SimpleBox<Integer> box2 = new SimpleBox<>(8);
11+
SimpleBox<String> box3 = new SimpleBox<>("Box");
12+
13+
int sum = box1.getValue() + box2.getValue();
14+
System.out.println(sum);
15+
16+
// talking about homework
17+
Integer[] integers = {4, 3, 2, 1, 6, 7};
18+
swapElements(2, 4, integers);
19+
20+
String[] strings = {"qwe", "dfa", "sdf", "fff"};
21+
swapElements(1, 3, strings);
22+
23+
// using RubberArray
24+
RubberArray<Integer> ints = new RubberArray<>();
25+
ints.add(15);
26+
ints.add(9);
27+
System.out.println(ints);
28+
29+
RubberArray<String> strs = new RubberArray<>();
30+
strs.add("Hello");
31+
strs.add("Java");
32+
System.out.println(strs);
33+
}
34+
35+
static <T> void swapElements(int idxSrc, int idxTrg, T[] array) {
36+
System.out.println(array[idxSrc]);
37+
System.out.println(array[idxTrg]);
38+
}
39+
40+
static <T> List<T> toList(T[] array) {
41+
return null;
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lesson7;
2+
3+
public class RubberArray<E> {
4+
private Object[] array;
5+
6+
public RubberArray() {
7+
array = new Object[0];
8+
}
9+
10+
public void add(E value) {
11+
Object[] newArray = new Object[array.length + 1];
12+
System.arraycopy(array, 0, newArray, 0, array.length);
13+
newArray[array.length] = value;
14+
array = newArray;
15+
}
16+
17+
public E get(int index) {
18+
return (E) array[index];
19+
}
20+
21+
public void remove(int index) {
22+
Object[] newArray = new Object[array.length - 1];
23+
System.arraycopy(array, 0, newArray, 0, index);
24+
System.arraycopy(array, index + 1, newArray, index, array.length - index - 1);
25+
array = newArray;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
StringBuffer sb = new StringBuffer("[");
31+
for (int i = 0; i < array.length; i++) {
32+
sb.append(array[i]);
33+
if (i < array.length - 1) {
34+
sb.append(", ");
35+
}
36+
}
37+
return sb.append("]").toString();
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lesson7;
2+
3+
public class SimpleBox<V> {
4+
private V value;
5+
6+
public SimpleBox(V value) {
7+
this.value = value;
8+
}
9+
10+
public V getValue() {
11+
return value;
12+
}
13+
}

0 commit comments

Comments
 (0)