簡體   English   中英

用2D數組排序

[英]Sorting with 2D Arrays

我需要將隨機數排序到2D數組中的幫助。 我必須將50個隨機數生成到數組的一列中,然后按順序(按升序或降序)對數字進行排序。 這是我到目前為止所擁有的,我是如此迷失。 請幫忙。

更新后的版本

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]);
        }
    }          
}
}

您可以使用自定義Comparator對2D數組的初始元素進行排序:

Arrays.sort(anArray, new Comparator<int[]>() {
    public int compare(int[] lhs, int[] rhs) {
        return lhs[0]-rhs[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;
}

}

您可以考慮2D數組1D進行排序。 讓我們考慮一個3x4的數組。 第一個元素的索引為0,第二個為1,第三為2,第四為3,第五為4,依此類推。

從一維索引轉換為二維的一般公式:

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

例如,第5個元素的1D索引為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;
}

並對數組進行排序,就好像是一維的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM