# 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. ### TheMasteredPanda

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?

Thank's,
Panda .

2. ### Darthmineboy

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 x 1
3. ### EndermansEnd

A for each loop would be the preferred way.
And also please use code tags^^

4. ### Darthmineboy

Yes, but we need the index to set the new value.

• Agree x 1
5. ### 1byte

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. ### TheMasteredPanda

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
Last edited: May 20, 2016
7. ### VirusBrandon

Use a For-Each and then have a integer field that starts at zero as a counter.

8. ### TheMasteredPanda

Is there a thread/name for this so I can read into it more?

10. ### FlyingLlama

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(); )
while(--len>-1)
{
somearray[len].stuff();
// do a thing
}
Forward interation. slightly more complex.
Code (Java):
int len = somearray.length; ( or list.size(); )
int index = -1;
while(++index<len)
{
somearray[index].things();
// do a thing
}

11. ### Spottedleaf

Use a mutable integer type...?

12. ### JoJoModding

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 x 2
13. ### ExpDev

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).

14. ### VirusBrandon

Not all Java programmers, new Java programmers, give the guy a break.

• Friendly x 1
15. ### FlyingLlama

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 x 1
16. ### VirusBrandon

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