2014-02-11 28 views
0

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

的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' 

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

编辑:

杰森建议下面的代码,其中的工作原理,但它通过整数不循环。 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); 
+5

'list.get(list.size())'应该总是触发一个异常,因为'list.size()'比最大合法索引多一个。你想达到什么目的?通过索引或迭代器(或使用增强的for循环,它使用迭代器)循环访问数组列表有什么问题? –

+0

除getString()之外,getConfig()的返回类型和该类型有哪些方法? –

回答

1

这是你想要的吗?

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); 
    } 
+0

循环索引需要'for(int i = 1; i <= 7; i ++)'复制OP代码的行为。 –

+0

好的。这是他想要的吗?我不知道。 – Jason

+0

我不知道OP在问什么。但你仍然需要从1开始循环,而不是0。 –

0

还不能评论,但我知道使用.sort()方法会把你的arrayList按顺序排列。然后你会循环