Here is a simpler version of the add(Object, int) method from Section 3.4.9. Under what circumstances will it fail? How do you fix this problem?

```
1 public void add( E e, int p ) {
2 if (( p < 0 ) || ( p > length ) )
3 throw new IndexOutOfBoundsException();
4 SLNode newnode = new SLNode ( e, null );
5
6 SLNode cursor = find( p – 1 );
7 addAfter( cursor, newnode );
8 length++;
9 }

```

If index is 0, find(index - 1) will cause an error (0-1 = -1). Here is the correction:

if ( p > 0 )
SLNode cursor = find(index-1);
else
SLNode cursor = head;

Computer Science & Information Technology

You might also like to view...

When using a(n) ________, Access prompts the user for input when the report is opened and then filters the report based on what the user keyed in

Fill in the blank(s) with correct word

Computer Science & Information Technology

One of the primary goals of a dashboard is to stay focused on a specific business problem. This is an example of the ________ design concept

A) Making Sure It Is Well Defined B) Keeping It Simple C) Knowing Your Users D) Defining Crucial KPIs

Computer Science & Information Technology