除去去年ArrayList的随机字符串 [英] Removing last random string from arraylist

查看:164
本文介绍了除去去年ArrayList的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个从文件显示的随机行的计划,但我有点新的ArrayList。我想它的显示后,但不能从文件从ArrayList中移除最previous随机线。这是因为我不希望它被重复做。这里是我的code:

I'm making a program that displays random lines from a file but I'm a bit new to arraylists. I want to remove the most previous random line from the arraylist after it's displayed but not from the file. This is because I don't want it to be repeated. Here's my code:

try {
    Random random = new Random();
    int randomInt = random.nextInt(50);
    FileReader fr = new FileReader(file);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    if (line.contains("!")) {
        List<String> lines = new ArrayList<>();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
            Random r = new Random();
            String rdmln = lines.get(r.nextInt(lines.size()));
            line1.setText("Line" + rdmln);

我希望能够从数组列表中删除'rdmln'后它已经被显示,使得它不会被再次显示。先谢谢了。

I want to be able to remove 'rdmln' from the arraylist after it has been displayed so that it won't be displayed again. Thanks in advance.

推荐答案

检查的 删除(INT指数) 功能。你的情况:

Check the remove(int index) function. In your case:

//your code
String rdmln = lines.remove(r.nextInt(lines.size()));
//the rest of your code

删除将它从列表中,并返回到您使用了。

remove will take it from the list and return it to you for use.

这篇关于除去去年ArrayList的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆