用于从 ArrayList#1 中删除出现在另一个 ArrayList#2 中并且在 ArrayList#1 中不唯一的行的代码
这篇文章是 的延续我之前的帖子。现在我有一个想要编译的代码。唯一的区别是,现在我使用自己的类 List
的列表,而不是 List
。特别要注意 Row 中的 hashCode
,因为它提供了编译错误。
public class Row {
private String key;
private Integer[] values;
public Row(String k,Integer[] v) {
this.key = k;
this.values = v;
}
public String getKey() {
return this.key;
}
public Integer[] getValues() {
return this.values;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
// object must be Row at this point
Row row = (Row)obj;
return ((key == row.key) && (values == row.values));
}
@Override
public int hashCode () { // HERE I HAVE A PROBLEM. DON'T KNOW HOW TO IMPLEMENT IT
return this.key;
}
}
public class Test {
public static void main(String[] args) {
List<Row> allRows = new ArrayList<Row>();
allRows.add(new Row("0",new Integer[]{1,2,3}));
allRows.add(new Row("0",new Integer[]{1,2,2}));
allRows.add(new Row("1",new Integer[]{1,2,3}));
allRows.add(new Row("2",new Integer[]{1,1,1}));
allRows.add(new Row("2",new Integer[]{1,1,1}));
List<Row> selectedRows = new ArrayList<Row>();
selectedRows.add(new Row("0",new Integer[]{1,2,3}));
selectedRows.add(new Row("2",new Integer[]{1,1,1}));
System.out.println(allRows);
System.out.println(selectedRows);
List<Row> refreshedRows = refreshRows(allRows,selectedRows);
System.out.println(refreshedRows);
}
private static List<Row> refreshRows(List<Row> allRows,List<Row> selectedRows) {
Set<Row> set1 = new HashSet<Row>();
Iterator<Row> it = allRows.iterator();
while(it.hasNext()) {
Row curr = it.next();
if (!set1.add(curr) && selectedRows.contains(curr)) {
it.remove();
}
}
return allRows;
}
}
结果,即 refreshedArray
,应该等于:
key = "0", values = {1,2,3}
key = "0", values = {1,2,2};
key = "1", values = {1,2,3};
key = "2", values = {1,1,1};
This post is the continuation of my previous post. Now I have a code that I'd like to compile. The only difference is that now I'm using lists of my own class List<Row>
instead of List<Integer[]>
. In particular look at hashCode
in Row, because it provides a compilation error.
public class Row {
private String key;
private Integer[] values;
public Row(String k,Integer[] v) {
this.key = k;
this.values = v;
}
public String getKey() {
return this.key;
}
public Integer[] getValues() {
return this.values;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
// object must be Row at this point
Row row = (Row)obj;
return ((key == row.key) && (values == row.values));
}
@Override
public int hashCode () { // HERE I HAVE A PROBLEM. DON'T KNOW HOW TO IMPLEMENT IT
return this.key;
}
}
public class Test {
public static void main(String[] args) {
List<Row> allRows = new ArrayList<Row>();
allRows.add(new Row("0",new Integer[]{1,2,3}));
allRows.add(new Row("0",new Integer[]{1,2,2}));
allRows.add(new Row("1",new Integer[]{1,2,3}));
allRows.add(new Row("2",new Integer[]{1,1,1}));
allRows.add(new Row("2",new Integer[]{1,1,1}));
List<Row> selectedRows = new ArrayList<Row>();
selectedRows.add(new Row("0",new Integer[]{1,2,3}));
selectedRows.add(new Row("2",new Integer[]{1,1,1}));
System.out.println(allRows);
System.out.println(selectedRows);
List<Row> refreshedRows = refreshRows(allRows,selectedRows);
System.out.println(refreshedRows);
}
private static List<Row> refreshRows(List<Row> allRows,List<Row> selectedRows) {
Set<Row> set1 = new HashSet<Row>();
Iterator<Row> it = allRows.iterator();
while(it.hasNext()) {
Row curr = it.next();
if (!set1.add(curr) && selectedRows.contains(curr)) {
it.remove();
}
}
return allRows;
}
}
The result, i.e. refreshedArray
, should be equal to:
key = "0", values = {1,2,3}
key = "0", values = {1,2,2};
key = "1", values = {1,2,3};
key = "2", values = {1,1,1};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作。尽管有一些细微的变化,大部分代码都是由 Netbeans IDE 7.0 生成的:
Try with the following. Despite minor changes, most of the code is generated by Netbeans IDE 7.0:
查看
hashcode()
的签名。它返回一个原始整数。您正在返回key
,其类型为String
。尝试这样的事情:你的 IDE 甚至可以为你生成。您可能应该阅读哈希码。你的 equals 方法看起来也错了。比较两个整数数组是否相等意味着什么?
Look at the signature for
hashcode()
. It returns a primitive integer. You are returningkey
which is of typeString
. Try something like this:which your IDE can even generate for you. You should probably read up on hashcodes. Your equals method looks wrong too. What is meant by comparing the two Integer arrays for equality?