从数组列表中删除最后一个随机字符串 [英] Removing last random string from arraylist

查看:46
本文介绍了从数组列表中删除最后一个随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个显示文件中随机行的程序,但我对数组列表有点陌生.我想在显示而不是从文件中删除 arraylist 中最近的随机行.这是因为我不想重复.这是我的代码:

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.

推荐答案

检查 remove(int index) 函数.在你的情况下:

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 会将其从列表中取出并返回给您使用.

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

这篇关于从数组列表中删除最后一个随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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