Highest Int Value

Discussion in 'Plugin Development' started by vasil7112, Jul 22, 2013.

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

    vasil7112

    Dear Developers,
    Lets say we have many values:
    int value1;
    int value2;
    .... [A year later] ....
    int value100;
    And i want to see which value is the highest.
    int value1 = 5;
    int value2 = 10; [Highest Value]
    int value100 = 3;
    Is there any easy way to do that?:p
    Also, if we have 2+ same high values? Can i use Random to select one of all of those same values?
     
  2. Offline

    psanker

    I... what? So you have a massive amount of independently declared 32-bit int fields. My first question is: why not use an int array (int[])? It's cleaner and doesn't require a dissertation of variable fields. Second: do I smell CS homework?

    The easiest to do this would be to have an int array and a int that stores the highest value found. Upon iteration through the array, check if the array index value is higher than the int with the highest found value. If so, update the highest value and continue on. If not, still just continue on.
     
  3. Offline

    vasil7112

    First: Can you please give me an example with the arraylist, Second: What is CS homework?

    EDIT: I found a faster solution for me, Math.max function.
     
  4. Offline

    1Rogue

    For the sake of the question: 2,147,483,647
     
    dark navi, Chiller and psanker like this.
  5. Offline

    vasil7112

    I know that the max int that you can have is 2.147.483.647 dude:p
    But i can get the max number out of many integers this way
    Math.max(int1,int2)
    And if there ar emore than 2 ints->
    Math.max(int1,Math.max(int2,int3))
     
  6. Offline

    Chiller

  7. Offline

    foodyling

    vasil7112 Iterating over each int in an array and storing a value called "int highest", every iteration you change highest to Math.max(int1, highest).
     
  8. Offline

    JPG2000

    foodyling Im almost positive that the thing you just said was not in english. The only thing I undertood abut that is change.
     
  9. Offline

    Chiller

  10. Offline

    dark navi

    You can do the max check yourself if you want to, since you're iterating over the entire thing anyways.

    Code:java
    1. ArrayList<Integer> numbers = new ArrayList<Integer();
    2. numbers.add(8);
    3. numbers.add(10);
    4. numbers.add(12315436);
    5. numbers.add(-45);
    6.  
    7. int maxValue = Interger.MIN_VALUE; // Start off at the smallest value possible. Can only go up from here!
    8.  
    9. for(Integer i : numbers)
    10. {
    11. if(i > maxValue)
    12. maxValue = i;
    13. }
    14.  
    15. Bukkit.getLogger().Log(Level.Info, "Max value: " + maxValue);
     
  11. Offline

    vasil7112

    Thanks alot sir:)
     
Thread Status:
Not open for further replies.

Share This Page