从以下数组列表中,我需要删除逗号并将每个元素添加到JTable中的单元格。例如,My JTable - 将有12列,第1行应填0 0 0 ....
请求您帮助我从列表中删除逗号并将元素添加到JTable中的单元格。
//代码
ArrayList list = new ArrayList();
list.add(" 0,0,0,4,2,0,2,0,1,0,0,0和#34;);
list.add(" 1,0,0,4,2,0,3,0,1,0,0,1和#34;);
list.add(" 2,0,0,4,1,0,2,0,1,0,0,0和#34;);
答案 0 :(得分:0)
ArrayList list = new ArrayList();
list.add("0 , 0 , 0 , 4 , 2 , 0 , 2 , 0 , 1 , 0 , 0 , 0");
list.add("1 , 0 , 0 , 4 , 2 , 0 , 3 , 0 , 1 , 0 , 0 , 1");
list.add("2 , 0 , 0 , 4 , 1 , 0 , 2 , 0 , 1 , 0 , 0 , 0");
//now go through the ArrayList and split each entry by ',' and add to a 2D array, JTable
int[][] JTable = new int[list.size()][];
for (int row = 0; row < list.size(); row++) {
//get an item from the ArrayList
String rowString = list.get(row);
//remove all the whitespaces from the string
rowString = rowString.replaceAll("\\s+","");
//split the string using "," as a delimiter
String[] items = rowString.split(",");
JTable[row] = new int[items.length];
for (int col = 0; col < items.length; col++) {
JTable[row][col] = Integer.parseInt(items[col]);
}
}
答案 1 :(得分:0)
以下代码正常运行!发布代码以帮助其他程序员....
//新逻辑 for(int row = 0; row&lt; recommendedTestSet.size(); row ++)
{
String rowString = recommendedTestSet.get(row);
rowString = rowString.replaceAll("\\s+","");
String[] items = rowString.split(",");
rtsTable.addRow(items);
}