Understanding OOP while iterating through a LinkedList for an assignement

Viewed 47

I still can't understand when and why should I use the "Obj obj = new Obj" and I don't get why it's so difficult for me. In this assignment I need to create a method as it follows:

"insertSorted: This method assumes the input LinkedList is already sorted in non-descending order (i.e.,such that each element is greater than or equal to the one that is before it, and inserts the input int value into the correct location of the list. Note that the method does not return anything, but rather modifies the input LinkedList as a side effect. If the input LinkedList is null, this method should simply terminate. This is the code you're starting with:

public static void insertSorted(LinkedList<Integer> list, int value) {

        /* IMPLEMENT THIS METHOD! */

    }

Let alone all the complications about iterating the LinkedList list, I don't even know how to start. Should I create a new LinkedList<Integer> newList = new LinkedList<Integer>(); so I can iterate through it right? Why though? If the list is given in the method signature should I assume that the Object is already created when the input is given in the method signature? I am really confused. It seems that I can't quite catch the whole Object programming thing.

2 Answers
Obj obj = new Obj

well if want to understand the new keyword in one line its like a contract in memory area where you can store the data (thats not all but enough to start).

public static void insertSorted(LinkedList<Integer> list, int value) {

    /* IMPLEMENT THIS METHOD! */

}

Now for this method you donot want to create any new object.

Q.Why?

Ans- when this method will called their is mandatory to pass some parameter to the method if not it will be a compile time error.

Passing values could be null.

Since the method returns void you should modify the list that is given as input. I'll show you why with an example

public static void insertSorted(LinkedList<Integer> list, int value) {

    LinkedList<Integer> list2 = new LinkedList<Integer>(list); //this means you are creating a new list of integers called list 2 with the same elements of the list "list" in the same order
    //some code to add value to list2 maintaing the sorting
    //no return needed

}

somewhere else you want to call this method

    LinkedList<Integer> list = new LinkedList<Integer>();

    list.Add(1);
    list.Add(2);
    list.Add(5);

    for (Integer i : list) System.out.println(i);
    //prints 1, 2 , 5
    insertSorted(list,4);
    for (Integer i : list) System.out.println(i);
    //still prints 1, 2 , 5!

if now you run this code with a debugger and you break in the method insertSorted right after you inserted the value in list2 you will see that list "list" remains as it was at the start of the method (which is (1,2,5)) and the list "list2" will be (1,2,4,5).

But the caller method knows nothing about list2!!!

When you declare a variable within a method it dies when the method ends (unless you return it). Of course you should pay attention to the "aliasing", but this is not the case.

Your requirements are very clear: your method must modify the input rather than creating a new list.

You can't understand why you should create a new object just because you don't have to. Someone just gave you a wrong suggestion :)

Now it's up to you to iterate the list and insert the integer in the right place :)

Related