2017-03-17 37 views
-3

我已经给出了一个返回二维对象数组的方法。如何使用下面给出的方法从对象数组中获取数据,并将其放入表或标签中以查看存储在其中的数据?这是我曾尝试没有成功:如何使用此方法从二维数组中获取数据?

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; 
} 

有什么建议?

UPDATE: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]; 

下面是完整的比较(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(); 
} 

}

+0

这是什么试图比较?它是比较一个字符串与另一个字符串?你的预期结果是什么? – user681574

+1

该方法不返回两个数组。它返回*一个*二维数组。您可以通过提供行和列索引来访问其成员 - 例如diffs [0] [0]和diffs [0] [1]。 –

+0

该方法从差异列表中提取“名称”元素。然后将这些提取的名称添加到双数组中,您可以通过调用array [i] [0] == array [i] [1]来访问每个元素,并比较它们以查看它们是否是相同的元素。您可以调用==或.equal来比较两个元素。 – Juniar

回答

0

我不是通过所有的代码去。我可以看到为什么你得到一个NullPointerException在你说的行中的原因是在difference.size()difference为空,所以你打电话给null上的方法。这很可能是因为我询问的地图differences没有与您的table作为关键字的映射(不太可能的解释是关键在那里,但与其关联的值是null)。

如果您确定键值对应存在(非null值),则需要查看将其放入其中的代码,以了解其原因。另一方面,如果你不想保证它在那里,你应该使用if - elsedifference进行null检查。如果differencenull,则可以从您的方法返回new Object[0][]

相关问题