-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataList.java
55 lines (54 loc) · 1.04 KB
/
DataList.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
import java.util.ArrayList;
import java.util.Collections;
/*
* Class to hold a list of points
* Keeps the points in an ArrayList
* Whenever a point is added, resorts
* to ensure they are in x-order
*/
public class DataList {
private ArrayList<Point> points;
public DataList() {
points = new ArrayList<Point>();
}
/*
* Two overloaded addPoint methods
* one takes the x y co-ordinate, makes
* a Point and then calls the other...
* ...which takes a Point as input and
* adds it to the list
*/
public void addPoint(int x, int y) {
Point p = new Point(x,y);
addPoint(p);
}
/*
* Add a point and then sort
*/
public void addPoint(Point p) {
points.add(p);
Collections.sort(points);
}
/*
* Get the ith point
*/
public Point getPoint(int i) {
return points.get(i);
}
/*
* Get the total number of points
*/
public int getN() {
return points.size();
}
/*
* Turn the whole thing into a string
*/
public String toString() {
String s = "";
for(Point p: points) {
s += p.toString() + "\n";
}
return s;
}
}