【问题标题】:Shuffling a 2d array while keeping columns elements together在将列元素保持在一起的同时对二维数组进行洗牌
【发布时间】:2015-07-26 20:30:53
【问题描述】:

所以我正在为一个班级做这个作业,但我一生都无法弄清楚如何使用 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;
  }
}

【问题讨论】:

    标签: java arrays multidimensional-array collections


    【解决方案1】:

    这不是解决您的问题的正确数据结构。您需要一个列表/数组对(例如,List&lt;Map.Entry&lt;String, String&gt;&gt;),然后在该列表上调用 Collections.shuffle()

    【讨论】:

    • 好吧,我还没有学过地图库。它真的很容易使用还是我必须编写更多方法才能使其正常工作?抱歉,Java 还是个菜鸟
    • 您需要的所有方法都已经存在。您通常只需要put 方法来首先填充地图,然后如果您想检索条目,则需要get 方法。 javadoc 将为您提供这些方法的所有详细信息以及更多信息。
    • 好的。谢谢!效果很好! (我已经在研究了。哈哈)
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多