简体   繁体   中英

initialized variable doesn't change?

Getting caught up on why this works. The variable biggest is initialized with a counter that starts at index 0 from the array list which is value of 3 . but when compared in the while loop it compares 3 whether 3 is more than 3 if not counter goes up. counter goes up but the initialized "biggest" variable remains the same. Why?

 public static int greatest(ArrayList<Integer> list) {
        System.out.println(list); 
        int counter = 0;

        int biggest = list.get(counter); // biggest starts at index 0 - value 3 // this counter seems to go up but the "biggest"
                                        // value remains the same when the counter below goes up.

        while (counter< list.size() ){// goes to while loop
            if ( list.get(counter)>biggest){
                biggest=list.get(counter);
            }

            counter++;
        }

        System.out.println(biggest);
        System.out.print(counter);  
        return biggest;   
    }            

    public static void main(String[] args) {
        ArrayList<Integer> lista = new ArrayList<Integer>();
        lista.add(3);
        lista.add(2);
        lista.add(71);
        lista.add(2);

        System.out.println("The greatest number is: " + greatest(lista));
    }

The biggest variable changes only if the current value in the loop is bigger than the value currently stored in biggest.

list.get(counter)>biggest

This is the reason why you don't see it always changing.

I've added a couple of prints to better show this concept, hoping you can use it for better understanding.

 public static int greatest(ArrayList<Integer> list) {
        System.out.println(list); 
        int counter = 0;

        int biggest = list.get(counter); // biggest starts at index 0 - value 3 // this counter seems to go up but the "biggest"
                                        // value remains the same when the counter below goes up.

        while (counter< list.size() ){// goes to while loop
            System.out.println("Current biggest value: " + biggest);
            System.out.println("Current iteration value: " + list.get(counter));
            if ( list.get(counter)>biggest){
                System.out.println("Current value is bigger than biggest! Changing biggest value...");
                biggest=list.get(counter);
            }

            counter++;
        }

        System.out.println(biggest);
        System.out.print(counter);  
        return biggest;   
    }            

    public static void main(String[] args) {
        ArrayList<Integer> lista = new ArrayList<Integer>();
        lista.add(3);
        lista.add(2);
        lista.add(71);
        lista.add(2);

        System.out.println("The greatest number is: " + greatest(lista));
    }

As a bonus I also want to share another version done with an arguably simpler for loop.

Note how here the value of max is set as the minimum possible value that an integer can have on purpose.

Doing so we can be sure that is will be surely lower of any other value found in the ArrayList.

Here's the example:

public int getMax(ArrayList<Integer> list){
    int max = Integer.MIN_VALUE;
    for(int i=0; i<list.size(); i++){
        if(list.get(i) > max){
            max = list.get(i);
        }
    }
    return max;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM