Just get the "latest" thing in a for-Loop?

Discussion in 'Plugin Development' started by Uniclaw, Jan 10, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Uniclaw

    Hi!

    How can i just get the latest thing in a for loop?
    So, for example i've a config like this:

    '1':
    cmd: 'say hello'
    '5':
    cmd: 'say hello you'
    '10':
    cmd: 'say hello you how'


    Now, if i check a thing like that:

    int a = //how i get the amount
    for(String s : thosekeys){
    if(a >= s_as_int){execute();}
    }
    that will execute all things if a is 0. How can i stop it and just execute the "latest" thing? so for example a is 6, i execute just the thng from 5 and not from 1..
     
  2. Offline

    Terradominik

    i''m not sure what you want exactly but i think that you can directly get the last object of "thosekeys"
     
  3. Offline

    Uniclaw

    Nope, the List/Set/Whatever is unsorted :/

    Okay, here in a example:
    In the config is the same thing like above.

    Now, the player can earn points with rightclicking items. Every point, a check-method is runned, wich get the last integer from the config wich is not too high and not too less and execute its command
     
  4. Offline

    fireblast709

    Code:java
    1. String[] keys = thosekeys.toArray(new String[0]).
    2. String last = keys[keys.length - 1];
    Best I can think of atm
     
  5. Offline

    Uniclaw

    fireblast709 But the list is unsorted :(! Its can also be

    10:
    1:
    5:
     
  6. Offline

    Terradominik

    then sort it :D
     
  7. Offline

    Uniclaw

    Even if i sort it - The problem is the same:
    if i sort
    10
    1
    5

    to

    1
    5
    10

    and get the latest thing, 10, but my integer is 6, nothing happens..
     
  8. Offline

    Terradominik

    oh now i know what you want simply use a if statement ...
    if (targetNumber >= thosekeys && targetNumber <= thosekeys[i+1])

    in the for loop you increase i
     
  9. Offline

    fireblast709

    You get the last number, so what is the rest of the problem? What is the exact relation to the 6? (what I read in the OP is that you want to execute it if it is >= 6, so 10 should execute)

    In your case, use for(int i = 0; i < thosekeys.length - 1; i++)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
Thread Status:
Not open for further replies.

Share This Page