2016-08-17 52 views
0

我试图在遍历JTable的行和列的迭代后显示数组的内容。我试过Arrays.toString(myTwoDimensionalArrayVariable),但它不会显示字符串值。显示2维对象数组的字符串值

我的目标是重复检查每一个目的地的行每列当用户试图从JTable,这就是为什么我要来显示阵列的内容添加行值JTable

列上的值是double,Stringint的组合。

int myRowCount = aJTableParameter.getRowCount(); 
int myColumnCount = aJTableParameter.getColumnCount(); 
Object[][] myRowValues = new Object[myRowCount][myColumnCount]; 


for (int j = 0; j < myRowCount; j++) { 
    for(int i = 0; i< myColumnCount; i++){ 
     myRowValues[j][i] = aDestinationTable.getValueAt(j, i); 
    } 
} 

System.out.println(Arrays.toString(myRowValues)); 

if (Arrays.asList(myRowValues).contains(column1Value) 
       && Arrays.asList(myRowValues).contains(column2Value) 
       && Arrays.asList(myRowValues).contains(column3Value) 
       && Arrays.asList(myRowValues).contains(column4Value)) { 
      JOptionPane.showMessageDialog(null, "Duplicate, try again."); 
}else{ 
     //do something else 
} 

我只得到这样的输出:

run: 
Successfully recorded login timestamp 
[] 
[[Ljava.lang.Object;@35fa3ff2] 
[[Ljava.lang.Object;@407c448d, [Ljava.lang.Object;@1e78a60e] 

是否有任何其他选择,使用二维数组?

我很感激任何帮助。

谢谢。

回答

1

IFF您的JTable细胞只包含字符串,您可以定义数组作为 String[][]代替 Object[][]并使用 aDestinationTable.getValueAt(j, i).toString()您的JTable内容填充它。

编辑:因为这不是这种情况(根据您的评论),它可能是最好使用一个列表,像这样:

List<List<Object>> objectList = new ArrayList<>(); 
    for (int j = 0; j < 2; j++) { 
     objectList.add(j, new ArrayList<>()); 
     for (int i = 0; i < 2; i++) { 
      if (i==0) objectList.get(j).add("string" + j + i); 
      if (i==1) objectList.get(j).add((double) 37.8346 * j * i); 
     } 
    } 

    System.out.println("OBJECT LIST: "+objectList); 

输出:

OBJECT LIST: [[string00, 0.0], [string10, 37.8346]] 

您的代码应像这样,然后:

List<List<Object>> myRowValues = new ArrayList<>(); 
    for (int j = 0; j < myRowCount; j++) { 
     myRowValues.add(j, new ArrayList<>()); 
     for (int i = 0; i < myColumnCount; i++) { 
      myRowValues.get(j).add(aDestinationTable.getValueAt(j, i)); 
     } 
    } 

    System.out.println(myRowValues); 
+0

感谢您的建议。对不起,我忘了说这是字符串,双打和整数的组合。 – p3ace

+0

非常感谢。我不认为我必须使用列表,我的知识仅限于ArrayLists和Arrays。我肯定会学习列表。我很欣赏这个例子。这解决了这个问题,因为我现在可以在else块上比较它。 – p3ace