2014-01-29 74 views
0

我有一個JTable的多維數組。但它不是在JTable正確顯示數據的顯示像這樣由升轉降的每一行:嵌套循環顯示不正確的多維數組

nidRbc nidRbc nidRbc nidRbc nidRbc nidRbc nidRbc nidRbc nidRbc 
INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER 
RBC identity RBC identity RBC identity RBC identity RBC identity RBC identity RBC identity RBC identity RBC identity 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
0:16382 0:16382 0:16382 0:16382 0:16382 0:16382 0:16382 0:16382 0:16382 
maxRouteLength maxRouteLength maxRouteLength maxRouteLength maxRouteLength maxRouteLength maxRouteLength maxRouteLength maxRouteLength 
NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 
Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route Maximum length (km) of a locked route 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
2 2 2 2 2 2 2 2 2 
0 0 0 0 0 0 0 0 0 
50 50 50 50 50 50 50 50 50 
###.### ###.### ###.### ###.### ###.### ###.### ###.### ###.### ###.### 
minRouteLength minRouteLength minRouteLength minRouteLength minRouteLength minRouteLength minRouteLength minRouteLength minRouteLength 
INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER 
Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route Minimum length (m) of a locked route 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
3 3 3 3 3 3 3 3 3 
0 0 0 0 0 0 0 0 0 
300 300 300 300 300 300 300 300 300 
0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 
maxMALength maxMALength maxMALength maxMALength maxMALength maxMALength maxMALength maxMALength maxMALength 
NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 NUMBER:3 
Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority Max length (km) for a Movement Authority 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
4 4 4 4 4 4 4 4 4 
0 0 0 0 0 0 0 0 0 
50 50 50 50 50 50 50 50 50 
###.### ###.### ###.### ###.### ###.### ###.### ###.### ###.### ###.### 
minMAExtension minMAExtension minMAExtension minMAExtension minMAExtension minMAExtension minMAExtension minMAExtension minMAExtension 
INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER 
Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA Min length (m) to extend an existing MA 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
5 5 5 5 5 5 5 5 5 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 
minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation minTrainSeparation 
INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER 
Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains Min distance (m) between two trains 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
6 6 6 6 6 6 6 6 6 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 0:1000 
siteOwner siteOwner siteOwner siteOwner siteOwner siteOwner siteOwner siteOwner siteOwner 
STRING STRING STRING STRING STRING STRING STRING STRING STRING 
Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) Country and owner of line (for communication identities) 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
7 7 7 7 7 7 7 7 7 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
[^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} [^/]{1,5}/[^/]{1,5} 
siteName siteName siteName siteName siteName siteName siteName siteName siteName 
STRING STRING STRING STRING STRING STRING STRING STRING STRING 
Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) Name of line (for communication identities) 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
8 8 8 8 8 8 8 8 8 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
[^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} [^/]{1,15} 
docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport docNoSiteDataReport 
STRING STRING STRING STRING STRING STRING STRING STRING STRING 
Document number for site data report Document number for site data report Document number for site data report Document number for site data report Document number for site data report Document number for site data report Document number for site data report Document number for site data report Document number for site data report 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
9 9 9 9 9 9 9 9 9 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList docNoBaliseList 
STRING STRING STRING STRING STRING STRING STRING STRING STRING 
Document number for balise list Document number for balise list Document number for balise list Document number for balise list Document number for balise list Document number for balise list Document number for balise list Document number for balise list Document number for balise list 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
10 10 10 10 10 10 10 10 10 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport docNoGeographicalReport 
STRING STRING STRING STRING STRING STRING STRING STRING STRING 
Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report Document number for geographical report 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 
11 11 11 11 11 11 11 11 11 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
nidC nidC nidC nidC nidC nidC nidC nidC nidC 
INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER 
Country identity Country identity Country identity Country identity Country identity Country identity Country identity Country identity Country identity 
0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 
12 12 12 12 12 12 12 12 12 
0 0 0 0 0 0 0 0 0 
NULL NULL NULL NULL NULL NULL NULL NULL NULL 
1:1023 1:1023 1:1023 1:1023 1:1023 1:1023 1:1023 1:1023 1:1023 

但我希望它顯示從左不右各數據從上往下

所以應該是這樣的:

nidRbc INTEGER RBC 1 1 1 0 NULL 0:16382 
maxRouteLength NUMBER:3 Maximum 1 1 2 0 50 ###.### 
minRouteLength INTEGER . ... . etc etc.. 

這是現在我的代碼:

// Create a list of node and compile the xpath expression 
      NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
        xmlDocument, XPathConstants.NODESET); 

      System.out.println("nodelist length : " + nodeList.getLength()); 
      System.out.println("constant length : " 
        + constants.getParamColumns().length); 

      // Create a new string for the array 
      arr = new String[nodeList.getLength()][constants.getParamColumns().length]; 

      // Loop through the list and save the data in a string array. 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       for (int j = 0; j < constants.getParamColumns().length; j++) { 

        System.out.println("content : " 
          + nodeList.item(i).getTextContent()); 
        arr[i][j] = nodeList.item(i).getTextContent(); 
        // System.out.println("Param : " + Arrays.toString(arr)); 

       } 
      } 
+0

@kai幾乎工作它顯示正確,但相同的數據的所有它從未顯示該行的時間:maxRouteLength NUMBER:3最大1 1 2 0 50 #### ## – Sembrano

回答

0

這是解決方案:

for (int i = 0; i < nodeList.getLength() 
        /constants.getParamColumns().length; i++) { 
       for (int j = 0; j < constants.getParamColumns().length; j++) { 

        arr[i][j] = nodeList.item(
          j + (i * constants.getParamColumns().length)) 
          .getTextContent(); 

       } 
      } 
0

如果你想反向索引順序,只是反向索引順序:

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument,XPathConstants.NODESET); 

System.out.printf("Nodelist Length:%1$d%nConstant Length:%2$d%n", 
        nodeList.getLength(), constants.getParamColumns().length); 

arr = new String[constants.getParamColumns().length][nodeList.getLength()]; 
for(int i = 0; i < nodeList.getLength(); i++) 
    for(int j = 0; j < constants.getParamColumns().length; j++) 
     System.out.printf("Content:%1$s%n", arr[j][i]=nodeList.item(i).getAttributes().item(j)); 

數組索引是從左向右關聯,這意味着最左邊的指數先解決。因此,在多維數組中,一個寫入array[rowNumber][columnNumber]

+0

這不工作先生。它只是獲得第一個項目並正確顯示它,但在第二行它應該獲得第二個項目,但它會獲得第一個項目。而在第三排,它再次獲得第一名。所以它解決了口渴問題,但不是所有問題。 – Sembrano

+0

@Sembrano現在應該被修復 – AJMansfield

+0

nope canot從節點轉換爲字符串 – Sembrano