0

I have an a ArrayList which I would like to display in order and loop continuously, but I'm not sure how to do so. I seem to only be able to use the specific type of String used in the current code, but if you know of a workaround so I can iterate it, that would be great.

The current code for the ArrayList and string is:

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

Config file (where integers come from) looks like this:

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

It currently infinitely displays the seventh integer's message.

EDIT:

Jason suggested the following code, which works, but it does not cycle through the integers. The "5" in BarAPI.setMessage(p, message, 5); is equal to five seconds.

    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);
2
  • 5
    list.get(list.size()) should always trigger an exception, since list.size() is one more than the maximum legal index. What are you trying to accomplish? What's the problem with looping through an array list either by index or with an iterator (or with an enhanced for loop, which uses an iterator)? Commented Feb 11, 2014 at 2:41
  • What is the return type of getConfig() and what methods does that type have, other than getString()? Commented Feb 11, 2014 at 2:45

2 Answers 2

1

Is this what you wanted?

  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);
  }
Sign up to request clarification or add additional context in comments.

7 Comments

The loop index needs to be for (int i = 1; i <= 7; i++) to replicate the behavior of OP's code.
Good catch. Is this what he wanted? I can't tell.
I don't know what OP is asking. But you still need to start the loop at 1, not at 0.
@Jason Closer, but it only displays the first one, and I should have been more clear about this, but it needs to display one at a time, and change every five seconds (which is built into the "BarAPI"), which it is not doing. It only seems to display number 7 from the ArrayList.
Updated the code. Think I understand what you want now.
|
0

Can't comment yet, but I know that using the .sort() method would put your arrayList in order. And then you'd loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.