【发布时间】:2017-03-17 20:01:02
【问题描述】:
我得到了一个返回二维对象数组的方法。如何使用下面给出的方法从对象数组中获取数据,并放入表格或标签中以查看存储在其中的数据?这是我尝试过但没有成功的方法:
Object[][] diffs = comp.getDifferences(comp.getName());
for(Object diff : diffs){
table.addItem(diff);
}
这是给我的方法:
public Object[][] getDifferences(String table) {
List<Tuple<Object, Object>> difference = differences.get(table);
Object[][] array = new Object[difference.size()][2];
for (int i = 0; i < array.length; i++) {
array[i][0] = difference.get(i).item1();
array[i][1] = difference.get(i).item2();
}
return array;
}
有什么建议吗?
更新:3:52 CST 3/17:
我想出了这个......
Table table = new Table();
Object[][] diffs = comp.getDifferences(comp.getName());
for(int i = 0; i < diffs.length; i++){
table.addItem(diffs[i][0]);
table.addItem(diffs[i][1]);
}
这也不起作用。我得到一个指向这条线的 NPE:
Object[][] array = new Object[difference.size()][2];
这是整个 Compare(comp) 类:
public class Compare implements Serializable {
private String name;
private Map<String, List<Tuple<Object, Object>>> differences = new HashMap<String, List<Tuple<Object, Object>>>();
public Compare(String name) {
this.name = name;
}
public Compare(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput ois = new ObjectInputStream(bis);
Compare compare = (Compare)ois.readObject();
this.name = compare.name;
this.differences = compare.differences;
bis.close();
ois.close();
}
public String getName() {
return name;
}
public boolean addDifference(String table, Object control, Object test) {
if (!differences.containsKey(table)) {
differences.put(table, new ArrayList<Tuple<Object, Object>>());
}
differences.get(table).add(new Tuple<Object, Object>(control, test));
return true;
}
public Object[][] getDifferences(String table) {
List<Tuple<Object, Object>> difference = differences.get(table);
Object[][] array = new Object[difference.size()][2];
for (int i = 0; i < array.length; i++) {
array[i][0] = difference.get(i).item1();
array[i][1] = difference.get(i).item2();
}
return array;
}
public byte[] toBytes() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();
byte[] bytes = bos.toByteArray();
bos.close();
oos.close();
return bytes;
}
private class Tuple<X, Y> implements Serializable {
private final X x;
private final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
public X item1() {
return this.x;
}
public Y item2() {
return this.y;
}
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Compare foo = new Compare("Test Compare");
foo.addDifference("AGREEMENT", 89, 90);
foo.addDifference("AGREEMENT", "foo", "bar");
foo.addDifference("ACCOUNT", 123, "Dog");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\Users\\uttbm01\\Documents\\Export\\foo.cmp")));
oos.writeObject(foo);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("C:\\Users\\uttbm01\\Documents\\Export\\foo.cmp")));
Compare bar = (Compare)ois.readObject();
ois.close();
System.out.println();
}
}
【问题讨论】:
-
这是要比较什么?是否将一个字符串与另一个字符串进行比较?您的预期结果是什么?
-
该方法没有返回两个数组。它返回 one 二维数组。您可以通过提供行和列索引来访问其成员——例如,diffs[0][0] 和 diffs[0][1]。
-
该方法确实从差异列表中提取“名称”元素。然后将这些提取的名称添加到一个双精度数组中,您可以通过调用 array[i][0] == array[i][1] 访问每个元素并比较它们以查看它们是否是相同的元素。您可以调用 == 或 .equal 来比较两个元素。
-
哇,已经因为提出问题而被否决了?惊人的。感谢那些试图通过在否决之前提供某种反馈来提供帮助的评论者。安迪·托马斯和朱尼尔,谢谢。你们是对的。您的回复为我指明了正确的方向。
-
另外,我只想列出所有差异并打印到表格或标签或任何东西上。就这些。不需要做任何比较。我只是想看看那里有什么。比较已经完成,差异存储在“comp”中。