我目前正在使用一个程序,每次运行程序时都会实例化三个数字变量。例如:
(companyID, groupID, hostID)
The 1st time when I run it
(int companyID = 1; int groupID = 1, int hostID = 1)
2nd time
(1, 1, 1)
3rd time
(1, 1, 1)
4th
(1, 2, 1)
5th
(6, 4, 3)
...
有没有一种方法可以在每次运行程序时保存数据集中的三个数字,同时防止以相同顺序重复的数字存储在数据集中?在上面的示例中,仅存储1,1,1的一个实例。
因此,存储在数据集中的最终结果仅为(1,1,1),(1,2,1),(6,4,3)。
感谢。
答案 0 :(得分:1)
试试这个:
<强> CODE 强>
import java.util.ArrayList;
import java.util.List;
public class DatasetUnique {
public static void main(String args[]){
// Data to insert
int[] one = {1,2,3};
int[] two = {2,3,4};
int[] three = {1,2,3};
int[] four = {7,8,9};
int[] five = {2,3,4};
// Data inserted
List<dataSet> allContent = new ArrayList<>();
// 1 - Insert data
insert(new dataSet(one), allContent);
insert(new dataSet(two), allContent);
insert(new dataSet(three), allContent);
insert(new dataSet(four), allContent);
insert(new dataSet(five), allContent);
// 2 - Print data
System.out.println(allContent.toString());
}
// Insert a dataset inside list if doesnt exists
private static void insert(dataSet dataSetToInsert, List<dataSet> allContent){
if(allContent.size() == 0){
allContent.add(dataSetToInsert);
}else{
for(dataSet ds: allContent){
if(dataSetToInsert.equals(ds)){
return;
}
}
allContent.add(dataSetToInsert);
}
}
// Class for each dataSet
private static class dataSet{
int firstNumber;
int secondNumber;
int thirdNumber;
public dataSet(int[] dataSet) {
super();
this.firstNumber = dataSet[0];
this.secondNumber = dataSet[1];
this.thirdNumber = dataSet[2];
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
dataSet other = (dataSet) obj;
if (firstNumber != other.firstNumber)
return false;
if (secondNumber != other.secondNumber)
return false;
if (thirdNumber != other.thirdNumber)
return false;
return true;
}
@Override
public String toString() {
return "\n" + firstNumber + " " + secondNumber + " "+ thirdNumber + "\n";
}
}
}
<强>输出强>
[
1 2 3
,
2 3 4
,
7 8 9
]
答案 1 :(得分:1)
你需要一些包装类。您可以存储3个整数或一组整数,这取决于您。
您需要实现Comparable接口
如果您将使用Set类来完成此任务,请注意HashSet将不起作用,因为它不是基于自然顺序。 TreeSet会。
这是您的类的简单代码示例。
当然,这不包括将类存储在文件中的部分,但允许您在工作时管理重复项。然后你可以将所有元素从set写入文件,它们将是唯一的。并从文件加载元素以便在开始时设置。
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static final class Tuple implements Comparable<Tuple> {
public int x1;
public int x2;
public int x3;
public Tuple(int x1, int x2, int x3) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
}
/**
* You don't need equal method to work with sets (and this is not even overriding basic equals method,
* but it's just an example of how you can easily compare your tuples
*/
public boolean equals(Tuple obj) {
return this.x1 == obj.x1 && this.x2 == obj.x2 && this.x3 == obj.x3;
}
/**
* Check javadoc for this, but it's just a way to tell if one tuple is more, equal or less then another
*/
@Override
public int compareTo(Tuple o) {
if (o.x1 != this.x1) {
return new Integer(o.x1).compareTo(this.x1);
}
if (o.x2 != this.x2) {
return new Integer(o.x2).compareTo(this.x2);
}
return new Integer(o.x3).compareTo(this.x3);
}
@Override
public String toString() {
return "[" + x1 + " " + x2 + " " + x3 + "]";
}
}
public static void main(String[] args) {
/**
* Note, that HashSet won't work because it doesn't depend on natural ordering of elements.
* TreeSet does, and it will work fine for your purpose.
*/
Set<Tuple> tuples = new TreeSet<>();
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(1, 2, 3));
tuples.add(new Tuple(1, 1, 1));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(2, 3, 4));
tuples.add(new Tuple(1, 2, 3));
for (Tuple tuple: tuples) {
System.out.println(tuple);
}
}
}`
这将是
的输出[2 3 4]
[1 2 3]
[1 1 1]
没有重复项放入Set。