繁体   English   中英

打印具有列和行的2D数组的元素

[英]Printing elements of a 2D array with columns and rows

我是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.");



}

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

在您的情况下,我建议再实现两个方法: sortByColumn(int[][] arr)sortByRow(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++];
    }
}

从给定的2D数组创建1D数组的方法:

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

暂无
暂无

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

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