【问题标题】:Printing elements of a 2D array with columns and rows打印具有列和行的二维数组的元素
【发布时间】:2018-06-02 06:11:18
【问题描述】:

我是 java 新手,我正在尝试按列和按行打印从选择排序算法中获得的元素。 未排序矩阵的预期输出可以是行 486 123 147 927 到 456(行)或 258(列) 135(未分类)789 369 提前致谢!由于我是学生,请不要专业的解决方案。

public void sort(boolean byColumn)
{
    double[] temp = null;
    double placeHolder = 0.0;
    int total = 0;
    int index = 0;

    /* Incrementing the total to use for the length of the temporary array. */
    for (int rows = 0; rows < mdArray.length; rows++) 
    {
        for (int columns = 0; columns < mdArray[rows].length; columns++)
        {
            total++;
        }
    }

    temp = new double[total];

    /* This nested for loop converts the indices of mdArray to one long temp array. */
    for (int rows = 0; rows < mdArray.length; rows++) 
    {
        for (int columns = 0; columns < mdArray[rows].length; columns++)
        {
            temp[index] = mdArray[rows][columns];
            index++;
        }
    }

    for (int i = 0; i < temp.length; i++) // Sorting the temporary array using selection sort. 
    {
        for (int j = i + 1; j < temp.length; j++)
        {
            if (temp[j] < temp[i])
            {
                placeHolder = temp[i];
                temp[i] = temp[j];
                temp[j] = placeHolder;
            }
        }
    }
    index = 0;

    if (byColumn) // Sorting the invoking array by column.
    {
        for (int rows = 0; rows < mdArray.length; rows++) // Puts sorted elements back into mdArray.
        {
            for (int columns = 0; columns < mdArray[rows].length; columns++)
            {
                mdArray[rows][columns] = temp[index];
                index++;
            }
        }

        for (int rows = 0; rows < mdArray.length; rows++)
        {
            System.out.println();
            for (int columns = 0; columns < mdArray[rows].length; columns++)
            {
                System.out.print(mdArray[rows][columns] + " ");
            }
        }
    }

    else if (!byColumn) // Sorting the invoking by row.
    {
        for (int columns = 0; columns < mdArray[columns].length; columns++)
        {
            for (int rows = 0; rows < mdArray.length; rows++)
            {
                mdArray[columns][rows] = temp[index];
                index++;
            }
        }

        for (int rows = 0; rows < mdArray.length; rows++)
        {
            System.out.println();
            for (int columns = 0; columns < mdArray[rows].length; columns++)
            {
                System.out.print(mdArray[rows][columns] + " ");
            }
        }
    }

    else if (byColumn && isRagged()) // 
        System.out.println("Cannot sort ragged arrays by column.");



}

【问题讨论】:

  • 还有什么不完全有效?我还没有检查它,但你正在尝试做的应该与你的代码一起工作,只要它没有错误......
  • 这两件事都是由相同的打印出来的:123 456 789
  • 我相信它们都是成行打印的。

标签: java loops multidimensional-array


【解决方案1】:

提示:如果您在代码中添加注释行,这绝对意味着最好将代码部分提取到单独的方法中。

在您的情况下,我建议再实现两个方法:sortByColumn(int[][] arr) 和 sortByRow(int[][] arr),因为乍一看使用 sort(arr, true) 并不太清楚(true 到底是什么意思)。

客户的公共方法(明确使用):

public static void sortByColumn(int[][] arr) {
    sort(arr, true);
}

public static void sortByRow(int[][] arr) {
    sort(arr, false);
}

排序实现(受保护,不能直接提供给客户端):

prvate static void sort(int[][] arr, boolean byColumn) {
    int[] temp = convertToSimpleArray(arr);

    selectionSort(temp);

    if (byColumn) {
        for (int row = 0, i = 0; row < arr.length; row++)
            for (int col = 0; col < arr[row].length; col++)
                arr[row][col] = temp[i++];
    } else {
        for (int col = 0, i = 0; i < temp.length; col++)
            for (int row = 0; row < arr.length; row++)
                if (col < arr[row].length)
                    arr[row][col] = temp[i++];
    }
}

从给定的二维数组创建一维数组的方法:

private static int[] convertToSimpleArray(int[][] arr) {
    int total = 0;

    for (int row = 0; row < arr.length; row++)
        for (int col = 0; col < arr[row].length; col++)
            total++;

    int[] res = new int[total];

    for (int row = 0, i = 0; row < arr.length; row++)
        for (int col = 0; col < arr[row].length; col++)
            res[i++] = arr[row][col];

    return res;
}

使用所需排序算法对数组进行排序的方法:

private static void selectionSort(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] >= arr[i])
                continue;

            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }
}

输入:

int[][] arr = {
        { 19, 44, 16, 39, 26 },
        { 42, 6, 30 },
        { 44, 37, 0, 46 },
        { 37, 42, 48, 19, 40 } };

输出:

sortByColumn(arr) provides:
     0  6 16 19 19
    26 30 37
    37 39 40 42
    42 44 44 46 48

sortByRow(arr) provides:
     0 19 37 42 46
     6 26 39
    16 30 40 44
    19 37 42 44 48

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多