簡體   English   中英

如何將陣列制作成2D陣列?

[英]How can I make an array to 2D array?

抱歉,我是Java的新手,也許我的問題有點愚蠢,但請幫助我。 所以,我在這里有2個數組:

char[] alphabet = {'A','B','C','D','E','F','G','H','I','J'
                   ,'K','L','M','N','O','P','Q','R','S','T'
                   ,'U','V','W','X','Y','Z'};     
char[] numberArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

然后我已經將兩個數組合並到一個新數組中,然后將其隨機化,例如輸出將是:

4 Z B 8 R W P F T 1 D H S L Q 2 N J 5 6 V 3 A C 0 G K U E X 7 O Y M 9 I 

我編寫的用於合並兩個數組的代碼如下:

public static char[]merge(char[]alphabet, char[]numberArray){
    char[] both = new char[alphabet.length+numberArray.length];
    int i;
    for(i=0; i<alphabet.length; i++)
        both[i] = alphabet[i];
    for(int j=0; j<numberArray.length; j++)
        both[i++]=numberArray[j]; return both;
}

然后從此輸出中,我想將所有這些元素制成帶有隨機元素的幾個2D數組。 像這樣

G 4
7 Y

要么

H T
U 8

依此類推……如何使輸出像這樣? 如何使其變成2x2陣列?

哦,要弄清楚,我想將其分成2個2x2數組中的1個以上,並自動生成,也許我需要循環才能生成它們。

閱讀此答案以了解如何在Java中創建2D數組。

如果您了解如何創建它,那么您將了解如何填充它。 然后,可以使用Random類隨機選擇元素,例如:

// generates an integer between 0 (inclusive) and arrayLength (exclusive)
new Random().nextInt(arrayLength); 

您可以按照此處所述,使用Fisher-Yates算法對數組進行混洗。

  // Implementing Fisher–Yates shuffle
  static void shuffleArray(char[] ar)
  {
    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

在將合並數組隨機化之后,這應該可以解決問題:

char[][][] your_array = new char[9][2][2]
for (int i = 0; i < 9; i++) {
    your_array[i][0][0] = randomized_array[4*i]
    your_array[i][0][1] = randomized_array[4*i + 1]
    your_array[i][1][0] = randomized_array[4*i + 2]
    your_array[i][1][1] = randomized_array[4*i + 3]
}

您可以像這樣訪問每個2x2數組: your_array[0]返回第一個, your_array[0]返回第二, your_array[0]

另一個答案

首先,您可以使用集合,而無需循環:

// just change char to Character 

List<Character> alpha =Arrays.asList(alphabet);
List<Character> numb =Arrays.asList(numberArray);
List<Character> all=new ArrayList<Character>();
all.addAll(alpha);
all.addAll(numb);
Character [] both2=all.toArray(new Character[all.size()]);

然后對於4個值,在所有(某些循環)之間隨機抽取4個索引:

// SELECTING
int how_many=10;
int total_size=all.size();

char[][][] ard2=new char[how_many][2][2];

for (int k=0;k<how_many;k++)
    {
    char[][] current_ard2=ard2[k];

    // Take 4 chars differents, by taking 4 different indexes
    int[] idx=new int[4];
        for (int j=0;j<4;j++)
            {
            boolean not_done=true; 

            while (not_done)
                {
                boolean done=true; // by default
                int candidate=(int) (Math.random()*total_size);

                // check precedent values
                for (int h=0;h<j;h++)
                    if (idx[h]==candidate)
                    {// lost !
                    done=false;
                    break;
                    }
                if (!done) continue; // candidate already exists

                // bingo
                idx[j]=candidate;
                not_done=false;
                }           
            } // for (int j=0;j<4;j++)

    current_ard2[0][0]=both2[idx[0]];
    current_ard2[0][1]=both2[idx[1]];
    current_ard2[1][0]=both2[idx[2]];
    current_ard2[1][1]=both2[idx[3]];

    System.out.println(current_ard2[0][0]+" "+current_ard2[0][1]+"\n"+current_ard2[1][0]+" "+current_ard2[1][1]+"\n");
    } // for (int k=0;k<how_many;k++)

希望能幫助到你

暫無
暫無

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

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