-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathTriple.java
More file actions
66 lines (56 loc) · 1.5 KB
/
Triple.java
File metadata and controls
66 lines (56 loc) · 1.5 KB
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
package array;
import list.Addible;
/**
* 稀疏矩阵非零元素的三元组类
*/
public class Triple implements Comparable<Triple>, Addible<Triple> {
int row, column, value;// 行号,列号,元素值,默认访问权限
//以行号,列号以及值构造三元组
public Triple(int row, int column, int value) {
super();
if (row < 0 || column < 0)
throw new IllegalArgumentException("稀疏矩阵元素三元组的行/列序号非正数");
this.row = row;
this.column = column;
this.value = value;
}
// 拷贝构造方法,赋值一个三元组
public Triple(Triple elem) {
this(elem.row, elem.column, elem.value);
}
// 返回三元组描述字符串
public String toString() {
return "(" + row + "," + column + "," + value + ")";
}
@Override
// 根据三元组位置比较两个三元组的大小,与元素值无关,约定三元组排序次序
public int compareTo(Triple elem) {
if (this.row < elem.row || this.row == elem.row
&& this.column < elem.column)
return -1;
if (this.row == elem.row && this.column == elem.column)
return 0;
return 1;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Triple))
return false;
Triple elem = (Triple) obj;
return this.row == elem.row && this.column == elem.column
&& this.value == elem.value;
}
@Override
// 两个三元组相加
public void add(Triple term) {
if (this.compareTo(term) == 0)
this.value += term.value;
else
throw new IllegalArgumentException("两项的指数不同,不能相加");
}
@Override
// 约定删除条件,不存储值为0的元素
public boolean removeable() {
return this.value == 0;
}
}