2017-03-17 184 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][]