在标签中显示字符串后如何从ArrayList中删除字符串?

时间:2019-01-13 15:21:56

标签: java sqlite javafx

我有一个标签和ArrayList。 ArrayList由sqlite列中的字符串组成。

当我在键盘上按“ 1”时,应将ArrayList中的随机字符串应用于标签中的文本,然后将其从ArrayList中删除。 主要目标是当我总是按“ 1”时不两次显示ArrayList中的字符串

我该怎么做?

我是否应该为此目的使用收藏夹中的另一个列表?

List<String> list = new ArrayList<>();
@FXML private Label lb_randomCard;


mainAnchor.setOnKeyPressed(event -> {
  switch (event.getCode()) {

    case DIGIT1:
      showRandomQuestionCat2();

      break;

  }
});

我尝试这样做,但是随机字符串仍然出现:

private void showRandomQuestionCat2() {

  Random rand = new Random();
  String randomString = list.get(rand.nextInt(list.size()));
  lb_randomCard.setText(randomString);
  list.remove(randomString);

 }

编辑: 我尝试使用Collections.shuffle,但是什么也没发生。

    Random rand = new Random();
    int index = rand.nextInt(list.size());
    String randomString = list.get(index);
    lb_randomCard.setText(randomString);
    list.remove(index);
    Collections.shuffle(list);

编辑2:

抱歉我的愚蠢!我粘贴了错误的代码,我的完整方法是:

private void showRandomQuestionCat2() {

    int n = (int) (Math.random() * 100);
      if (n < 70) {
        Random rand = new Random();
        int index = rand.nextInt(list.size());
        String randomString = list.get(index);
        lb_randomCard.setText(list.get(index));
        index++;
        list.remove(index);

      } else if (n < 80) {
        Random rand = new Random();
        int index2 = rand.nextInt(listSentence.size());
        String randomString = listSentence.get(index2);
        lb_randomCard.setText(listSentence.get(index2));
        index2++;
        listSentence.remove(index2);

        }
    }

我删除时

int n = (int) (Math.random() * 100);

所有随机事物正常进行。

通过在方法开始时创建此随机对象,我希望有70%/ 30%的机会显示来自两个数组的字符串。

但是现在我不知道该如何做到偶然但没有重复

EDIT3:

这是显示重复字符串显示方式的视频:

Video

这是我的完整代码,包含两个数组:

@FXML  private AnchorPane mainAnchor

@FXML  private Label lb_randomCard;

List<String> list = new ArrayList<>();

List<String> listSentence = new ArrayList<>();

@FXML
public void initialize(URL location, ResourceBundle resources) {

//key commands
mainAnchor.setOnKeyPressed(event -> {
  switch (event.getCode()) {

    case DIGIT1:
      generateChar();
      Collections.shuffle(list);
      Collections.shuffle(listSentence);
      showRandomQuestionCat2();
      showRandomCard();

  }
});

 }

//put all values from SQLite table category1 to ArryayList
private void getAllQuest() {


  try {
    Connection conn = DbConnection.getConnection();
    pst = conn.prepareStatement(pq.getGetQuestions());
    rs = pst.executeQuery();
    while (rs.next()) {
      list.add(rs.getString("question"));
    }
  } catch (SQLException e) {
    e.printStackTrace();
  }
}

  //put all values from SQLite table sentences to ArryayList
  private void getAllSentences() {

    try {
      Connection conn = DbConnection.getConnection();
      pst = conn.prepareStatement(pq.getGetSentences());
      rs = pst.executeQuery();
      while (rs.next()) {
        listSentence.add(rs.getString("sentence"));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

//set random string from both Arrays and remove after that
  private void showRandomQuestionCat2() {

    int n = (int) (Math.random() * 100);
      if (n < 70) {
        Random rand = new Random();
        int index = rand.nextInt(list.size());
        String randomString = list.get(index);
        lb_randomCard.setText(list.get(index));
        index++;
        list.remove(index);

      } else if (n < 80) {
        Random rand = new Random();
        int index2 = rand.nextInt(listSentence.size());
        String randomString = listSentence.get(index2);
        lb_randomCard.setText(listSentence.get(index2));
        index2++;
        listSentence.remove(index2);

        }
    }

1 个答案:

答案 0 :(得分:1)

我建议您按索引删除而不是按值删除。

The following packages will be DOWNGRADED:

    mkl:                                2019.0-118              --> 2018.0.3-1
    python:                             3.7.0-hea74fb7_0        --> 3.6.8-h9f7ef89_0

Proceed ([y]/n)? n

编辑: 我用这个简单的例子模拟了您的代码,没有重复

private void showRandomQuestionCat2() {

  Random rand = new Random();
  int index = rand.nextInt(list.size());
  String randomString = list.get(index);
  lb_randomCard.setText(randomString);
  list.remove(index);

 }

在旁注中,public class test { static List<String> list = new ArrayList<>(); static List<String> listSentence = new ArrayList<>(); static Random rand = new Random(); public static void main (String []args) { list.add("hi1"); list.add("hi2"); list.add("hi3"); list.add("hi4"); listSentence.add("bye1"); listSentence.add("bye2"); listSentence.add("bye3"); listSentence.add("bye4"); for (int i=0; i<4; i++) showRandomQuestionCat2(); } private static void showRandomQuestionCat2() { int n = rand.nextInt(100); if (n < 70) { int index = rand.nextInt(list.size()); String randomString = list.get(index); System.out.println(list.get(index)); list.remove(index); } else { int index2 = rand.nextInt(listSentence.size()); String randomString = listSentence.get(index2); System.out.println(listSentence.get(index2)); listSentence.remove(index2); } } } 后跟if (n<70)不会给您70%/ 30%的机会