我有一个 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/