0

I need help with sorting Random numbers into a 2D array. I have to generate 50 random numbers into a column of the array, then sort the numbers in order (ascending or descending). This is what I have so far and am so lost. Please Help.

UPDATED VERSION

public static void main(String[] args) 
{
    int rows = 2;
    int columns = 50;

    int[][] anArray = new int[rows][columns];

    Random rand = new Random();

    for (int i = 0; i < anArray.length; i++) 
    {
        for (int j = 0; j < anArray[0].length; j++) 
        {
            int n = rand.nextInt(100);
            anArray[i][j] = n;
        }
    }

    int []temp;
    for (int i=0;i<anArray.length;i++)
        {  
            for (int j=0;j<anArray.length-i;j++ )
            {
                if (anArray[i][j]>anArray[i][j+1])
                {  
                    temp =anArray[j];
                    anArray[j+1]=anArray[j];
                    anArray[j+1]=temp;
                }
            }
        } 

    for (int i = 0; i < anArray.length; i++) 
    {
        for (int j=0;j<anArray.length-i;j++ )
        { 
            System.out.println(anArray[i][j]);
        }
    }          
}
}
4

3 回答 3

1

您可以使用自定义对 2D 数组的初始元素进行排序Comparator

Arrays.sort(anArray, new Comparator<int[]>() {
    public int compare(int[] lhs, int[] rhs) {
        return lhs[0]-rhs[0];
    }
});
于 2012-11-14T04:24:57.237 回答
0

首先,您需要嵌套 for 循环才能将随机数正确插入二维数组。我还更新了我的回复以显示应该如何进行排序。希望这可以帮助!

编辑以满足下面评论中提到的要求。

import java.util.Arrays;
import java.util.Random;


public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    int rows = 2;
    int columns = 50;

    int[][] anArray = new int[rows][columns];

    Random rand = new Random();
    //initialize the first row only
    for (int j = 0; j < anArray[0].length; j++) 
    {
       int n = rand.nextInt(100);
       anArray[0][j] = n;
    }
    System.out.println("-----------Before the Sort----------------");
    for (int i = 0; i < anArray.length; i++) 
    {
        for (int j = 0; j < anArray[0].length; j++)
        {               
           System.out.print(anArray[i][j] + ", ");  //format any way you want
        }
        System.out.println(); //to make each row print on a new line.
    }
    anArray = mySort(anArray);
    System.out.println("-----------After the Sort----------------");
    for (int i = 0; i < anArray.length; i++) 
    {
        for (int j = 0; j < anArray[0].length; j++)
        {               
           System.out.print(anArray[i][j] + ", ");  //format any way you want
        }
        System.out.println(); //to make each row print on a new line.
    }
}

private static int[][] mySort(int[][] anArray) {
    int [][] result = new int[anArray.length][anArray[0].length];
    int thisRow[] = getRow(anArray, 0);
    Arrays.sort(thisRow);
    for(int j = 0; j < thisRow.length; j++){
        result[0][j] = anArray[0][j];
        result[1][j] = thisRow[j];
    }
    return result;
}

private static int[] getRow(int[][] anArray, int row) {
    int thisRow[] = new int[anArray[row].length];
    for(int j = 0; j < anArray[row].length; j++){
        thisRow[j] = anArray[row][j];
    }
    return thisRow;
}

}

于 2012-11-14T04:28:39.773 回答
0

您可以通过考虑 2D 数组 1D 进行排序。让我们考虑一个 3x4 数组。第一个元素的索引是 0,第二个是 1,第三个是 2,第四个是 3,第五个是 4,等等。

从一维索引转换为二维的通用公式:

row_index = _1D_index % nRows;
col_index = _1D_index % nCols;

例如,第 5 个元素的一维索引为 4,获取行:4 % 3 = 1,获取 col,4 % 4 = 0,因此您的元素位于 1,0。这一切有什么意义?现在你可以做一个函数

int GetAt(int index)
{
    return array[index % nRows][index % nCols];
}

以及类似的东西:

void Swap(int index1, int index2)
{
   int r1 = index1 % nRows;
   int c1 = index1 % nCols;
   int r2 = index2 % nRows;
   int c2 = index2 % nCols;
   int temp = array[r1][c1];
   array[r1][c1] = array[r2][c2];
   array[r2][c2] = temp;
}

并对数组进行排序,就好像它是一维的:)

于 2013-07-07T01:57:18.340 回答