#can someone explain to me how this finds the smallest number in a arraylist?

1 messages · Page 1 of 1 (latest)

boreal falcon
#

Hey everyone im trying to grasp why this finds the smallest number the code was given to me earlier in the material and just copied it in but before moving on i would like to understand it here is the code


import java.util.ArrayList;
import java.util.Scanner;

public class IndexOfSmallest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // create an array list to hold input numbers
        ArrayList<Integer> list = new ArrayList<>();

        // read inputs from user until 9999 is entered
        while (true) {
            int input = Integer.valueOf(scanner.nextLine());
            if (input == 9999) {
                break;
            }
            // add the input to the array list
            list.add(input);
        }

        // initialize index variable to first element in the array list
        int index = list.get(0);

        // loop through each element in the array list and find the smallest value
        for (int i = 0; i < list.size(); i++) {
            int number = list.get(i);
            if (index > number) {
                index = number;
            }
        }

        // print out the smallest number
        System.out.println("The smallest number: " + index);

        // loop through each element in the array list and find the indices of the smallest number
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == index) {
                // if the element matches the smallest value, print its index
                System.out.println(index + " is at index " + i);
            }
        }
    }
}
wispy topazBOT
#

<@&987246399047479336> please have a look, thanks.

wispy topazBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

boreal falcon
#

i had chat gpt add the comments to explain it but it didnt really give me what im looking for

gentle ferry
#

walk through the code manually, top to bottom

boreal falcon
#

i have im just confused with why list.size() helps in this case

#

doesnt it just shoot out the number of inputs in the list?

#

OH

#

i get it now

#

kind of

buoyant rune
#

first you look for the smallest value in the list, and then you look for it's index (that's what your code does)

boreal falcon
#

i understand the index part of it

#

the smallest number part is the code i copied in

#

im trying to understand it now im stuck

#
                index = number;
#

i dont understand this

buoyant rune
#

the index variable in that if is actually the value an element in the list.
int index = list.get(0);
This line gets the first value of the list

Perhaps you should rename index to value for it to make more sense to you

buoyant rune
# boreal falcon ```if (index > number) { index = number; ```

This if compares the value you have in your index variable with the value in the number variable (the variable that's going through the list).
If you find a smaller variable in the list you update your index variable to the new smallest value

boreal falcon
#

if index ends up = number doesnt that just give the greatest number?

#

since it would just constantly change to the greater number cause of index > number?

strange path
#

@boreal falcon

#

You can just sort the array using. Sort()

#

and get the first index

boreal falcon
#

i would do that

#

but i havent learned it

#

so i dont wanna skip ahead

strange path
#

No it's a function in array list

boreal falcon
#

i havent learned it in the mooc course yet

strange path
#

Else what you can do is you can run a loop

boreal falcon
#

the code i have works

strange path
#

And make a control variable, compare 2 indexed of the array

boreal falcon
#

i just dont understand how it works

strange path
#

Can I see it?

boreal falcon
#

like i try to break down the smallest number and follow it step by step but does not make sense to me no matter what

#

sure

#

import java.util.ArrayList;
import java.util.Scanner;

public class IndexOfSmallest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // create an array list to hold input numbers
        ArrayList<Integer> list = new ArrayList<>();

        // read inputs from user until 9999 is entered
        while (true) {
            int input = Integer.valueOf(scanner.nextLine());
            if (input == 9999) {
                break;
            }
            // add the input to the array list
            list.add(input);
        }

        // initialize index variable to first element in the array list
        int index = list.get(0);

        // loop through each element in the array list and find the smallest 
        //value
        for (int i = 0; i < list.size(); i++) {
            int number = list.get(i);
            if (index > number) {
                index = number;
            }
        }

        // print out the smallest number
        System.out.println("The smallest number: " + index);

        // loop through each element in the array list and find the indices of 
        //the smallest number
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == index) {
                // if the element matches the smallest value, print its index
                System.out.println(index + " is at index " + i);
            }
        }
    }
}
strange path
#

It just loop through all indexes

#

And compares them and sets it to the control variable

#

Till it reaches end of the loop

boreal falcon
#

i just dont see where it exactly determines that its the smallest number tho

#

or should say understand

#

for (int i = 0; i < list.size(); i++) {

this cycles through the list

strange path
#

It happens after the while loop

boreal falcon
#

int number = list.get(i);

this gets the most current number

#
        for (int i = 0; i < list.size(); i++) {
            int number = list.get(i);
            if (index > number) {
                index = number;
#

thats my main confusion

strange path
#

See it gets the first index of the array list and compares it with the second, if the second is higher index = number and again it checks the third index of array list with the variable 'index' carrying the second index of array list, this happens until end of array is reached

boreal falcon
#

OH

#

so index is basically just constantly updated to the smallest number

#

?

#

i think im just gonna move on honestly i will eventually understand i guess

#

just not clicking for me

#

i get it now

#

thank you

buoyant rune
#

if that's it then write this: /help-thread close