Natalie_94
Natalie_94

Reputation: 79

Add method java

I want to add a method add(int index, E element) in Java, that inserts a specified element at a specified index in the list and shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). But I guess something is wrong with the indices in my code in the for-loop. Any ideas how to solve it?

public class SingleLinkedList<E> implements ISingleLinkedList<E> {
    Node head;
    int size = 0;
    @Override
    public void add(int index, E element) throws IndexOutOfBoundsException {
        Node newNode = new Node(element);
        if(head == null && index == 0) {
            head = newNode;
        }
        else if (index == 0 && head != null) {
            Node tempNode = new Node(element);
            tempNode.setmNextNode(head);
            head = tempNode;
        }
        else {
            Node tempNode = head;
            for(int i = 1; i<index; i++) {
                tempNode = tempNode.getmNextNode();
            }
            /**Node newNode = new Node(element);**/
            newNode.setmNextNode(tempNode);
            tempNode.setmNextNode(newNode);


        }
        size++;
    }
}

My code for the Node class is:

public class Node<E> {
private E mElement;
private Node<E> mNextNode;

Node(E data) {

    this.setmElement(data);
}
public E getmElement() {
    return this.mElement;
}
public void setmElement(E element) {
    this.mElement = element;
}
public Node<E> getmNextNode()
{
    return this.mNextNode;
}
public void setmNextNode(Node<E> node)
{
    this.mNextNode = node;
}

The problem is that I have a JUnit test that fails when adding this method and I do not know what more I need to add in order to pass the test.

    @Test
public void testAddWithIndexesToListWith5Elements() {

    int listSize = 5;
    // First create an ArrayList with string elements that constitutes the test data
    ArrayList<Object> arrayOfTestData = generateArrayOfTestData(listSize);
    // Then create a single linked list consisting of the elements of the ArrayList
    ISingleLinkedList<Object> sll = createSingleLinkedListOfTestData(arrayOfTestData);

    // Add new elements first, in the middle and last to the ArrayList of test data
    // and the single linked list
    try {
        arrayOfTestData.add(0, 42);
        arrayOfTestData.add(3, "addedElement1");
        arrayOfTestData.add(7, "addedElement2");
        sll.add(0, 42);
        sll.add(3, "addedElement1");
        sll.add(7, "addedElement2");            
    }
    catch (Exception e) {
        fail("testAddWithIndexesToListWith5Elements - add() method failed");
    }

    // Check that the contents are equal
    for (int i = 0; i < sll.size(); i++) {
        assertEquals(arrayOfTestData.get(i), sll.get(i));
    }
}

Upvotes: 0

Views: 179

Answers (2)

dan1st
dan1st

Reputation: 16338

Your question is pretty unclear but I think I can see a problem.

If index is not 0, the you will iterate through the nodes until the index is reached.

If there are not enough elements in the list, you will reach the end of the list before the index where you want to insert the element.

In this case,

tempNode = tempNode.getmNextNode();

will set tempNode to null.

In the next iteration, this line will throw a NullPointerException.

You can bypass this issue by testing if tempNode.getmNextNode(); is null.

If that is the case, the element will just be inserted at the end/that point or will not be inserted.

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147154

    newNode.setmNextNode(tempNode);
    tempNode.setmNextNode(newNode);

This is just going to create a cycle. It looks like your newNode should point to tempNode.getmNextNode() or something along those lines.

Upvotes: 2

Related Questions