Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what happens when you get an element in a list that does not exist?

I’m trying to get some clarification on what what happens when you try to retrieve and element in an index that does not exist.

List<List<Integer>> triangle = new ArrayList<List<Integer>>();

    //first row is always 1 and only 1
    triangle.add(new ArrayList<>());
    triangle.get(0).add(1);
    
    //loop through the numbers of rows and add them previous rows
    for(int rowNums = 1; rowNums < numRows; rowNums++){
        //creating new row
        List<Integer> row = new ArrayList<>();

        //previous row, used to add
        List<Integer> prevRow = triangle.get(rowNums - 1);

        //first element in the pascal triangle is 1
        row.add(1);

        for(int i = 1; i < rowNums; i++){
            row.add(prevRow.get(i - 1) + prevRow.get(i));

        }
        //last element in a pascal triangle is always 1
        row.add(1);

        //add the row to the triangle list
        triangle.add(row);
    }
    
    return triangle;

This code solves a pascal triangle, however, I’m confused at the nested for loop. I know that it looks at the previous rows and add the two elements together. But let say I’m in row 1, I would look back at the previous row (which is 0) and add elements at index 0 and 1 together. but index 1 does not exist in row 0. I hope this is clear.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are correct that an "IndexOutOfBoundsException" error is thrown when you try to retrieve an item with an index that does not exist in a list". However, in the code you provided, the "for" loop starts at index 1 and not index 0, so it never tries to retrieve an element at index 0.

The loop condition is "i < rowNums", which means that the loop will go from index 1 to (but not including) the value of "rowNums". In other words, if "rowNums" is 1, the loop will not be executed at all because the condition "i < rowNums" is not met. If "rowNums" is 2, the loop is executed once with "i" equal to 1, i.e. an attempt is made to retrieve elements at index 0 and index 1 of the previous row (i.e. the first row). In this case, index 1 is not present in the first row, but the code does not attempt to retrieve it because the loop condition prevents execution when "i" equals 0.

So the code is safe from "IndexOutOfBoundsException" errors and correctly calculates the values in each line of the Pascal triangle by adding the corresponding elements from the previous line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading