Solved Looping Through a String List Backwards?

Discussion in 'Spigot Plugin Development' started by Username1234, Apr 27, 2017.

Thread Status:
Not open for further replies.
  1. Is it possible to loop through a string list backwards? Here is my code:
    Code (Text):
    -
    So, this would flip around the entire scoreboard, mess it up, and make the order backwards. So, is there a way to loop backwards or is there another way to add text to a scoreboard?
     
    #1 Username1234, Apr 27, 2017
    Last edited: Apr 28, 2017
  2. #2 spammy23, Apr 27, 2017
    Last edited: Apr 27, 2017
    • Agree Agree x 1
  3. Do I put that before the loop or after? And in this case I need to store the String list (from config) to an arraylist and then call the method right?
     
  4. the intention is to delete the last item, however, to do so, he is using the same mechanics you'd use for your issue as well. (at least how you specified it)
     
  5. Lol! Who cares what the end goal is. The method to get there is in line with your interests. Look how they iterated backwards and you'll learn how to do it too.

    Imagine you want to learn to fly a plane. Someone is trying to show you how, but you ignore them because they're flying to Spain and your plan is to fly to China. Thats how you're acting here.. :S
     
    • Agree Agree x 3
    • Like Like x 1
  6. "Very destructive and inefficient"
     
  7. I am still very confused, help??
     
  8. Choco

    Moderator

    Preeeetty sure you were given the answer in the link that you neglected to learn from:
    TL;DR: Read it, learn from it, adapt it to what you need. This is what good programmers do
    http://stackoverflow.com/questions/19233466/how-to-iterate-from-last-to-first-an-arraylist
     
  9. what about this:

    Code (Java):

    for(int i = yourlist.size(); i > 0; i--){
      yourlist.get(i);
    }
     
  10. In this case I hope the List is either small or it implements RandomAccess for the performance :p
    Yeah, I'm a performance-nerd, I'm sorry, move on.
     
    • Agree Agree x 2
  11. Okay, let me clear this up.
    I want to loop through a String List in a configuration file, and I want to add each line to a scoreboard in the correct order.
     
  12. Quoting it here such as I think you aren't able to see it:
    List iterators can be used to iterate your list backwards, too -- like this:
    Code (Text):
    ListIterator<String> listIter = myList.listIterator(myList.size());
    while (listIter.hasPrevious()) {
        String prev = listIter.previous();
        // Do something with prev here
    }
    The initial index of the iterator should be equal to the index of the last element + 1. As the docs say: "An initial call to previous would return the element with the specified index minus one."
     
    • Like Like x 1
  13. @Xearox gave you code to iterate backwards through a list while keeping the original order intact, Why can't you get the list from the config using
    Code (Java):
    getConfig().getStringList(/*Parameters*/)
     
    • Agree Agree x 1
  14. What do I do with prev?
    Because I can't. Bukkit's scoreboard system is like this, and what am I supposed to do about it?
     
  15. Code (Text):
            for (String string : Trampoline.getInstance().getMessagesFile().getStringList("scoreboard")) {

                scoreboardData.add(string);
                Collections.reverse(scoreboardData);
                Score score = o.getScore(string);
                score.setScore(0);
            }
    This is what I am doing right now although this is very destructive.
     
  16. I have a strange feeling you have no idea about what you're doing.

    Save the list of 'messages' to a List, and reverse that list (if you really wish to do it that way..) BEFORE iterating over it.
    I'd still suggest doing it the way either @Xearox (preferably) or @F8te suggested, they might very well be more efficient.
     
  17. Why
    Code (Text):
    i > 0
    You will skip over the first index of 0. You mean
    Code (Text):
    i >= 0
     
    • Agree Agree x 1
  18. I don't think you know what you're doing lol, StringList returns a List what stops you from doing List#size and using List#get. I suggest you should learn some java before jumping into the bukkit api
     
Thread Status:
Not open for further replies.