How to run though a list of Integer's and do the same Algorithm

Discussion in 'Spigot Plugin Development' started by TheMasteredPanda, May 20, 2016.

  1. Hello,

    Ok so I am currently developing a plugin that allows people to have a bank, every hour they play they get an x amount of money on top of that. But I am stuck, I am currently working on the Algorithm that allows me to do this, but I do not know how to run though a list of Integer's and do the same Algorithm to each one. So how would I do this?

    Panda :).
  2. List<Integer> list; //Initialize it
    int size = list.size();
    for(int i = 0; i < size; i++)
    Integer value = list.get(i); //Get the value at index
    value += 10; //Algorithm: add 10
    list.set(i, value); //Set the new value at index
    • Like Like x 1
  3. A for each loop would be the preferred way.
    And also please use code tags^^
  4. Yes, but we need the index to set the new value.
    • Agree Agree x 1
  5. What about starting a task when a Player X joins and then every hour, you can get the Bank of the player and add some money to it... Don't forget to cancel the task, when the player leaves...

  6. Can't be some money, the money they put in calculates the amount that is added to their bank per hour. If I just added some fixed amount to their bank, it wouldn't actually fuel the economy inspiration that I am trying to create on my own server.
    #6 TheMasteredPanda, May 20, 2016
    Last edited: May 20, 2016
  7. Use a For-Each and then have a integer field that starts at zero as a counter.
  8. Is there a thread/name for this so I can read into it more?
  9. Java programmers really do not know how to write an incrementing/decrementing loop? Incredibly sad.

    Backwards iteration. Also safe to use this to delete list entries inside the loop (which is impossible for for-each or forward iteration)
    Code (Java):
    int len = somearray.length; ( or list.size(); )
        // do a thing
    Forward interation. slightly more complex.
    Code (Java):
    int len = somearray.length; ( or list.size(); )
    int index = -1;
        // do a thing
  10. Use a mutable integer type...?
  11. People do not know for loops:
    Code (Text):
    for(int i = 0; i < array.length, i++){
        //Do stuff
    (This may be more code but its 1. easier to read and 2. takes the same amount of time from a bytecode level of perspective.)
    • Winner Winner x 2
  12. I'm not sure if I've understood this correctly, but wouldn't a Map be more suitable?

    Considering each player have a bank, the map would look like UUID, Integer (or something like that).

    It would be easy to loop through (you could even just get a player's bank directly and set value).
  13. Not all Java programmers, new Java programmers, give the guy a break.
    • Friendly Friendly x 1
  14. while and for loops used to be part of what people learned before they even wrote their first piece of code.. I guess that is too much to expect in the modern world of API-spoonfeeding and shortcuts.
    • Agree Agree x 1
  15. Minecraft server plugin development is not really a good place to start learning Java, because @FlyingLlama is correct above, that many projects that programmers work on do not APIs that you can call.

    But yeah, @TheMasteredPanda,
    you really need to do some research and not give up so easily, what you are
    asking is incredibly simple, so search Google, struggle for a awhile, then ask for help.

    Struggle like the rest of us once did. ;)