具有嵌套循环的多维数组无法正确显示

时间:2014-01-29 11:42:44

标签: java jtable nested-loops

我有一个用于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));

                }
            }

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分: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();

                }
            }