从List中调用随机值元素

时间:2015-04-14 14:55:32

标签: java list

我希望能够从listarray字符串中调用随机字符串。我想要这样做的原因是能够组成一个故事,例如,每次都是不同的文本。到目前为止,我的方式是:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Story {

    Random myRandom = new Random();
    int index = myRandom.nextInt(10);

    public static void main(String[] args) {
        List<String> adjective = new ArrayList<String>();
        adjective.add("efficacious");
        adjective.add("propitious");
        adjective.add("trenchant");
        adjective.add("arcadian");
        adjective.add("effulgent");

        Story obj = new Story();

        System.out.println("This is going to be one " + obj.getRandAdjective(adjective) + " story.");
    }

    public String getRandAdjective(List<String> adjective) {
        int index = myRandom.nextInt(adjective.size());
        return adjective.get(index);
    }
}
  1. 考虑到这一点,有什么更有效的结构方法 是否会添加lists
  2. 是否可以将代码结构化为更短的代码 版本obj.getRandAdjective(adjective)写在。{ 故事(主要是为了可读性)?

3 个答案:

答案 0 :(得分:1)

我建议在这个类中添加所有列表作为静态变量,而不是在main方法中包含一堆列表。

如果你已经知道列表中你想要的单词用这些单词声明它们,而不是添加。您可以添加方法addWordToList(List<String> list)或类似的东西,以便在运行时添加单词

例如:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Story {

Random myRandom = new Random();
int index = myRandom.nextInt(10);
static List<String> adjective = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};
static List<String> noun = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};
static List<String> verb = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};

//etc... change the words in the lists

public static void main(String[] args) {
    Story obj = new Story();

    System.out.println("This is going to be one " + obj.getRandWord(adjective) + " story.");
}

public String getRandWord(List<String> words) {
    int index = myRandom.nextInt(adjective.size());
    return words.get(index);
}
}

如果这将成为一个更大的程序,你可能最好制作一个只有单词列表的静态类,你可以通过Class.List

访问它们

这将使代码更清晰,更有条理

答案 1 :(得分:1)

这是将你正在做的事情概括为各个词性的一种方法。我把这些类放在一起,以便更容易粘贴。你应该创建这些单独的公共类。

package com.ggl.testing;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public enum PartsOfSpeech {
    ADJECTIVE, ADVERB, ARTICLE, CONJUNCTION, INTERJECTION, NOUN,              
    PREPOSITION, PRONOUN, VERB
}

class Word {

    private final PartsOfSpeech partOfSpeech;

    private final String word;

    public Word(PartsOfSpeech partOfSpeech, String word) {
        this.partOfSpeech = partOfSpeech;
        this.word = word;
    }

    public PartsOfSpeech getPartOfSpeech() {
        return partOfSpeech;
    }

    public String getWord() {
        return word;
    }

}

class Sentence {

    private List<Word> words;

    private Random random;

    public Sentence() {
        this.words = createWordList();
        this.random = new Random();
    }

    private List<Word> createWordList() {
        List<Word> words = new ArrayList<Word>();
        words.add(new Word(PartsOfSpeech.VERB, "run"));
        // add the rest of the words here

        return words;
    }

    public Word getWord(PartsOfSpeech partsOfSpeech) {
        List<Word> subList = getSubList(partsOfSpeech);
        int index = random.nextInt(subList.size());
        return subList.get(index);
    }

    private List<Word> getSubList(PartsOfSpeech partsOfSpeech) {
        List<Word> subList = new ArrayList<Word>();
        for (Word word : words) {
            if (word.getPartOfSpeech() == partsOfSpeech) {
                subList.add(word);
            }
        }

        return subList;
    }

}

答案 2 :(得分:0)

您可以使用Math.Random生成0.0到1.0之间的随机数

如果你稍微调整它并玩它,你可以用它来做你需要的。像这个例子:

    List<String> adjective = new ArrayList<String>();
    adjective.add("efficacious");
    adjective.add("propitious");
    adjective.add("trenchant");
    adjective.add("arcadian");
    adjective.add("effulgent");

    System.out.println(adjective.get((int) Math.floor(Math.random() * adjective.size())));

输出List

的随机元素