-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeList.java
66 lines (56 loc) · 1.17 KB
/
EdgeList.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
public class EdgeList {
private Edge[] edges;
private int currentLocation;
public EdgeList(int initialLength) {
edges = new Edge[initialLength];
currentLocation = 0;
}
public int length() {
return currentLocation;
}
public Edge get(int index) {
if(index >= edges.length) {
return null;
}
return edges[index];
}
public void add(Edge e) {
try {
edges[currentLocation] = e;
currentLocation += 1;
return;
}
catch(Exception error) {
increaseSizeOfArray();
edges[currentLocation] = e;
currentLocation += 1;
return;
}
}
private void increaseSizeOfArray() {
int newSize = edges.length*2;
Edge[] temp = copyArray(edges);
edges = new Edge[newSize];
for(int i=0; i<temp.length; i++) {
edges[i] = temp[i];
}
}
private Edge[] copyArray(Edge[] es) {
Edge[] e = new Edge[es.length];
for(int i=0; i<es.length; i++) {
e[i] = es[i];
}
return e;
}
public String toString() {
String s = "(";
for(int i=0; i<edges.length;i++) {
if(edges[i] != null) {
s += "["+edges[i].getEndNode().getLabel();
s += ","+ edges[i].getDistance()+"] ";
}
}
return (s+")");
}
public Edge[] getList() { return edges; }
}