java - 按顺序循环 ArrayList<String>

标签 java string arraylist

我有一个 ArrayList,我想按顺序显示它并连续循环,但我不知道该怎么做。我似乎只能使用当前代码中使用的特定类型的字符串,但如果您知道解决方法以便我可以迭代它,那就太好了。

ArrayList 和字符串的当前代码是:

  public void showBarChanging(final Player p)
  {
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    {
      public void run()
      {
        ArrayList<String> list = new ArrayList<String>();
        list.add(DCBar.this.getConfig().getString("1").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("2").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("3").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("4").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("5").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("6").replaceAll("&", "§"));
        list.add(DCBar.this.getConfig().getString("7").replaceAll("&", "§"));

        String message = (String)list.get(list.size());

        BarAPI.setMessage(p, message);
      }
    }, 0L, 100L);
  }

配置文件(整数的来源)如下所示:

1: '&bTest 1'
2: '&bTest 2'
3: '&bTest 3'
4: '&bTest 4'
5: '&bTest 5'
6: '&bTest 6'
7: '&bTest 7'

目前它无限地显示第七个整数的消息。

编辑:

Jason 建议使用以下代码,该代码可以工作,但不会循环遍历整数。 BarAPI.setMessage(p, message, 5); 中的“5”等于五秒。

    ArrayList<String> list = new ArrayList<String>();
    for (int i=1;i<=7;i++){
        list.add(DCBar.this.getConfig().getString(Integer.toString(i)).replaceAll("&", "§"));
    }

    String message = (String)list.get(list.size()-1);

    BarAPI.setMessage(p, message, 5);

最佳答案

这是你想要的吗?

  public void showBarChanging(final Player p)
  {
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    {

      private static int count=1;
      public void run()
      {
           if (count>7) count=1;

           String string = DCBar.this.getConfig().getString(Integer.toString(count++));
           string = string.replaceAll("&", "§");

           BarAPI.setMessage(p, string, 5);   

      }
    }, 0L, 100L);
  }

关于java - 按顺序循环 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691973/

相关文章:

java - 直接在ArrayList中获取输入

java - 最终静态数组列表在函数中发生了变化

java - java中使用栈将对象添加到数组列表中

java - 与hadoop2.6.0一起部署的tachyon0.8.2,但IPC版本不匹配

java - 术语 "Instance variable"和 "variables declared in Interfaces"之间的区别

java - 使用 double 的二元运算符 '>' 的操作数类型错误

java - 如何将 JSON 映射到 JAXB 对象?

c++ - 使用 gets(a) 而不是 cin.getline(a,20) 有什么好处?

c++ - 从 C++ 中的类指针对象调用方法(字符串)

安卓多语言支持 : string formatting issue due to different placeholder counts