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...

A combination of keys that perform a command, such as Ctrl + I, is called a keyboard ________

A) rule B) shortcut C) function D) task

Computer Science & Information Technology

If host A is able to successfully ping router R1 but not router R2, which of the following is guaranteed?

A) Port security is not filtering frames sent between host A and R1. B) Port security is filtering frames sent by R2. C) R2 itself is down. D) STP has not placed the right ports in a forwarding state.

Computer Science & Information Technology

What are the four JFrame constructors?

What will be an ideal response?

Computer Science & Information Technology

What is a major advantage of DeviceNet, an open network intended to link low-level devices?

What will be an ideal response?

Computer Science & Information Technology