Java 如何使用此方法从二维阵列获取数据?
我得到了一个返回二维对象数组的方法。如何使用下面给出的方法从对象数组中获取数据,并将其放入表或标签中以查看其中存储的数据?这是我尝试过但没有成功的方法:Java 如何使用此方法从二维阵列获取数据?,java,arrays,for-loop,Java,Arrays,For Loop,我得到了一个返回二维对象数组的方法。如何使用下面给出的方法从对象数组中获取数据,并将其放入表或标签中以查看其中存储的数据?这是我尝试过但没有成功的方法: Object[][] diffs = comp.getDifferences(comp.getName()); for(Object diff : diffs){ table.addItem(diff); } 这就是我得到的方法: public Object[][] getDifferences(String table) {
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;
}
下面是整个Comparecomp类:
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();
}
}我不会通读你所有的代码。我可以理解为什么在你说的行中会出现一个NullPointerException,原因是in difference.size difference是null,所以你在对null调用一个方法。这很可能是因为我查询的映射(差异)没有将表作为键的映射。不太可能的解释是键在那里,但与它关联的值为null
如果您确定键值对应该具有非空值,则需要检查将其放入其中的代码,以了解为什么没有。另一方面,如果您不想保证它存在,那么应该使用If-else对差异进行空检查。如果差异为null,例如,您可以从方法返回新对象[0][]。这是要比较什么?它是在比较一个字符串和另一个字符串吗?您预期的结果是什么?该方法没有返回两个数组。它返回一个二维数组。您可以通过提供行和列索引来访问其成员-例如,diff[0][0]和diff[0][1]。该方法会从差异列表中提取名称元素。然后将这些提取的名称添加到一个双数组中,通过调用Array[i][0]==Array[i][1]并比较它们是否是相同的元素,可以访问每个元素。您可以调用==或.equal来比较两个元素。哇,问问题已经被否决了吗?令人惊叹的感谢那些试图在否决投票前提供某种反馈的评论者。安迪·托马斯和朱尼尔,谢谢。你们说得对。你的回答指引了我正确的方向。此外,我只想列出所有的差异,并打印到表格或标签上,或任何东西。仅此而已。不需要做任何比较。我只是想看看那里有什么。比较已经完成,差异存储在comp中。
Object[][] array = new Object[difference.size()][2];
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();
}