简体   繁体   English

多维数组列交换

[英]Multidimensional array column swap

After identifying that elements in a specific column of a multidimensional array are not numbers I'm looking at swapping two columns of this multidimensional array. 在确定多维数组的特定列中的元素不是数字之后,我要交换此多维数组的两列。

Basically I would like to swap the contents of column 4 with those in column 6 after determining whether elements in column 6 are numbers or not so if elements in column 6 are not numbers then create new array and start inserting old array contents in new array but once the column gets to the 4th column of the old array it places this element in the 6th column of the new array and vice versa. 基本上,在确定第6列中的元素是否为数字之后,我想将第4列的内容与第6列中的内容交换,因此,如果第6列中的元素不是数字,则创建新数组并开始在新数组中插入旧数组内容,但一旦该列到达旧数组的第4列,它将将该元素放置在新数组的第6列中,反之亦然。

Below is my attempt regarding the above. 以下是我关于上述内容的尝试。 I've been able to successfully identify if the elements in my column 6 are numbers or not. 我已经能够成功识别第6列中的元素是否为数字。 I now have doubts regarding the logic behind swapping the elements in column 4 with those in 6. 我现在对将第4列中的元素与第6列中的元素交换背后的逻辑有疑问。

//!<
public String[][] sortedByDateUsageStatsData(String[][] unsortedUsageStatsData) {
    System.out.println("\nbtnUsageStats - sortedByDateUsageStatsData(String[][] unsortedUsageStatsData)");

    for (int row = 0; row < unsortedUsageStatsData.length; row++) {
        for (int col = 0; col < 7; col++) {
            //System.out.println("unsortedUsageStatsData[" + row + "][" + col + "] => " + unsortedUsageStatsData[row][col]);
        }

        // Check if content in return code column i.e. column 6 is number or a string.
        // If a string then swap column 4 content with column 6 and pass this array with swapped columns as the final array
        if(!isNumericString(unsortedUsageStatsData[0][6])) {
            //System.out.println("unsortedUsageStatsData[0][6] is NOT a number i.e. " + unsortedUsageStatsData[0][6]);

            break;
        }

        System.out.println("\n");
    }

    System.out.println("\nExited for loop as unsortedUsageStatsData[0][6] is NOT a number\n");

    sortedUsageStatsData = new String[unsortedUsageStatsData.length][7];

    for (int row = 0; row < unsortedUsageStatsData.length; row++) {
        for (int col = 0; col < 7; col++) {
            //System.out.println("\nBefore swap unsortedUsageStatsData[" + row + "][" + col + "] => " + unsortedUsageStatsData[row][col]);

            sortedUsageStatsData[row][col] = unsortedUsageStatsData[row][col];

            if(col == 4) {
                System.out.println("\nBefore swap sortedUsageStatsData[" + row + "][6] => " + unsortedUsageStatsData[row][6]);                      
                sortedUsageStatsData[row][6] = unsortedUsageStatsData[row][4];
                System.out.println("\nAfter swap sortedUsageStatsData[" + row + "][6] => " + unsortedUsageStatsData[row][6]);                       

                sortedUsageStatsData[row][4] = unsortedUsageStatsData[row][6];
                System.out.println("\nAfter swap sortedUsageStatsData[" + row + "][4] => " + unsortedUsageStatsData[row][4]);                       
            }

            System.out.println("\nAfter swap sortedUsageStatsData[" + row + "][" + col + "] => " + unsortedUsageStatsData[row][col]);
        }

        System.out.println("\n");

    }

    return sortedUsageStatsData;
}

Any help is appreciated. 任何帮助表示赞赏。

There's a problem with your loop. 您的循环有问题。 In the iteration where col == 6 you override the value of sortedUsageStatsData[row][6] you had already set before (when col was 4). col == 6的迭代中,您将覆盖之前已经设置的sortedUsageStatsData[row][6]的值(col为4时)。

There are a few ways to change this, I'll name two: 有几种方法可以改变这种情况,我将列举两个:

  1. Iterate from 0 to 5 only: for (int col = 0; col < 6; col++) . 仅从0到5进行迭代: for (int col = 0; col < 6; col++)

  2. Set values of columns 4 and 6 in corresponding iterations: 在相应的迭代中设置第4列和第6列的值:

      if(col == 4) { sortedUsageStatsData[row][4] = unsortedUsageStatsData[row][6]; } else if (col == 6) { sortedUsageStatsData[row][6] = unsortedUsageStatsData[row][4]; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM