簡體   English   中英

改組2D數組,同時將列元素保持在一起

[英]Shuffling a 2d array while keeping columns elements together

因此,我正在為一個類進行此分配,而我一輩子都無法弄清楚如何使用Collections.shuffle()方法對由狀態及其大寫字母組成的數組進行混排。 現在,當我對數組進行混洗時,它將對所有內容進行混洗。 我需要它來洗凈一切,而且還必須使國家與首都之間的關系保持在一起。 (即

[阿拉巴馬州] [蒙哥馬利] [阿拉斯加] [朱諾] ...

不應該變成這樣的東西

[阿拉巴馬州] [阿拉斯加],[朱諾] [蒙哥馬利],...

要么

[阿拉斯加] [蒙哥馬利],[朱諾] [阿拉巴馬州],...

有什么我想念的嗎? 這是到目前為止的工作代碼...

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Random;
/** 
* @author mstashev 
* 
* Rewrite Programming Exercise 8.37 to store the pairs of states and 
* capitals so that the questions are displayed randomly. 
* 
* Exercise 8.37 
* 
* Write a program that repeatedly prompts the user to enter a capital 
* for a state. Upon receiving the user input, the program reports 
* whether the answer is correct. Assume that 50 states and their 
* capitals are stored in a 2D array. The program prompts the user to 
* answer all state's capitals and displays the total correct count. The 
* user's answer is not case-sensitive. 
*/ 
public class RandomStateCapitalQuestionnaire {
/**
 * 
 */
public static void main(String[] args) {
    // create a 2D array storing all 50 states and capitals
    String[][] twoDStatesAndCapitals = {
            { "Alabama", "Alaska", "Arizona", "Arkansas", "California",
                    "Colorado", "Connecticut", "Delaware", "Florida",
                    "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana",
                    "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
                    "Maryland", "Massachusetts", "Michigan", "Minnesota",
                    "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
                    "New Hampshire", "New Jersey", "New Mexico", "New York",
                    "North Carolina", "North Dakota", "Ohio", "Oklahoma",
                    "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
                    "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
                    "Virginia", "Washington", "West Virginia", "Wisconsin",
                    "Wyoming" },
            { "Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento",
                    "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta",
                    "Honolulu", "Boise", "Springfield", "Indianapolis",
                    "Des Moines", "Topeka", "Frankfort", "Baton Rouge",
                    "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul",
                    "Jackson", "Jefferson City", "Helena", "Lincoln",
                    "Carson City", "Concord", "Trenton", "Santa Fe", "Albany",
                    "Raleigh", "Bismark", "Columbus", "Oklahoma City", "Salem",
                    "Harrisburg", "Providence", "Columbia", "Pierre",
                    "Nashville", "Austin", "Salt Lake City", "Montpelier",
                    "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne" }            
        };//End of 2D array
    Random rnd = new Random();

    System.out.println(Arrays.deepToString(twoDStatesAndCapitals));


    List<List<String>> statesAndCapitals = twoDArrayToList(twoDStatesAndCapitals);
    Collections.shuffle(statesAndCapitals, rnd);


    System.out.println(statesAndCapitals);

    /*
     i = 0;
    for (i = 0; i < NUMBER_OF_US_STATES; i++) {
        System.out.println("Enter the Capital of the State listed: " + statesAndCapitals[i][0]);
        userInput = input.next();
        if (statesAndCapitals[i][1].equals(userInput)) {
            correctStates++;
            System.out.println("Yes that is the capital of " + statesAndCapitals[i][0] + ".");
        }//end of if

        else {
            System.out.println("No that is not the capital of " + statesAndCapitals[i][0] + ".");
            i++;
        }//end of else
    }//end of for loop

    // counts the number of correct guesses.
    System.out.println("Total number of capitals answered correctly: " + correctStates + ".");
    i++;
    return;

    */

}

public static <T> List<List<String>> twoDArrayToList(T[][] twoDArray){
    List<T> list = new ArrayList<T>();
    for (T[] array : twoDArray) {
        list.addAll(Arrays.asList(array));
    }
    return (List<List<String>>) list;
  }
}

這不是解決您問題的正確數據結構。 您需要一個成對的列表/數組(例如List<Map.Entry<String, String>> ),然后在該列表上調用Collections.shuffle()

暫無
暫無

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

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