用arrayList单词随机填充2d数组

时间:2012-04-16 22:29:42

标签: java arraylist multidimensional-array

我坚持使用wordSearch程序。我正在尝试使用用户输入的arrayList中的单词填充二维数组。有人能帮助我吗?

import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }


    public void fill() {

        int i, j, row, col;
        boolean added;


        for (i = 0; i < puzzleWords.size(); i++) {
            row = (int) (Math.random() * this.gridDimensions);
            col = (int) (Math.random() * this.gridDimensions);
            added = false;
            ???

1 个答案:

答案 0 :(得分:0)

Random类有nextInt(int n)方法,List接口有size()方法。

生成一个介于0和列表大小之间的随机索引,并从列表中的结果索引中获取单词。