-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.java
52 lines (41 loc) · 1.09 KB
/
Demo.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
package com.faisal;
public class Demo {
private int[] items;
private int count;
public Demo(int length) {
if (length <= 0)
throw new IllegalArgumentException();
items = new int[length];
}
public void insert(int item) {
if (count == items.length) {
int[] newItems = new int[items.length * 2];
for (int i = 0; i < count; i++)
newItems[i] = items[i];
items = newItems;
}
items[count++] = item;
}
public void removeAt(int index) {
if (index >= count)
throw new IllegalArgumentException();
// index: 4
// i: 5
// 4 <- 5
// 5 <- 6
// 6 <- 7
for (int i = index; i < count; i++)
items[i] = items[i + 1];
count--;
}
public int indexOf(int item) {
for (int i = 0; i < count; i++)
if (items[i] == item)
return i;
return -1;
}
public void print() {
for (int i = 0; i < count; i++)
System.out.println(items[i]);
}
}