我有一个ArrayList,我想按顺序显示并循环,但我不知道该怎么做。我似乎只能使用当前代码中使用的特定类型的String,但如果你知道一个变通方法,所以我可以迭代它,那将是很好的。
ArrayList和string的当前代码是:
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);
答案 0 :(得分: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);
}
答案 1 :(得分:0)
无法发表评论,但我知道使用.sort()
方法会使您的arrayList按顺序排列。然后你循环