Question:
Linked list example won't compile - please help!?
brilliant_moves
2012-07-12 04:00:13 UTC
The following example of a linked list - downloaded from http://www.roseindia.net/java/ - doesn't compile. Can anyone correct it? I'm using JDK1.7

import java.util.*;

public class LinkedListDemo {
public static void main(String[] args) {
LinkedList link=new LinkedList();
link.add("a");
link.add("b");
link.add(new Integer(10));
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());

link.addFirst(new Integer(20));
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());

link.addLast("c");
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());

link.add(2,"j");
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());

link.add(1,"t");
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());

link.remove(3);
System.out.println("The contents of array is" + link);
System.out.println("The size of the linked list is" + link.size());
}
}
Three answers:
Steve
2012-07-12 04:11:08 UTC
First, make sure you're importing the right LinkedList (This could be why you're getting the error re: parameters):

import java.util.LinkedList



Then change this line:

LinkedList link=new LinkedList();



to:

LinkedList link=new LinkedList();



You need to tell Java what kind of things you will store in your linked list... in this case I chose object because you add strings and ints... if it was just ints, then you could change it to:

LinkedList link=new LinkedList();



Hope that helps



Cheers



Steve



PS: for the record, when I was learning Java I got a lot of information, and a lot of headaches from RoseIndia... there is some useful stuff there, and some shocking stuff.



PPS: Glad you got it working :) Feel free to accept the answer if it helped...
Silent
2012-07-12 04:48:34 UTC
You're using example code from a very old version of Java — before 2003.



In those days, you couldn't tell Java what type of things were going to go into a collection like a LinkedList. Now, with a technique called generics, you can. Declare your list like this:



LinkedList link = new LinkedList();



Exactly like that. You need the part in both places.



If it still doesn't compile, then you've probably discovered a second, unrelated problem. You need to tell us what the compiler error message is if you want us to help you with that.
James Bond
2012-07-12 04:09:42 UTC
Problem is that you are using deprecated methods. Still you can run the program after compiling with Xlint. See my workout



C:\appu>javac LinkedListDemo.java

Note: LinkedListDemo.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.



C:\appu>javac -Xlint LinkedListDemo.java

LinkedListDemo.java:6: warning: [unchecked] unchecked call to add(E) as a member

of the raw type java.util.LinkedList

link.add("a");

^

LinkedListDemo.java:7: warning: [unchecked] unchecked call to add(E) as a member

of the raw type java.util.LinkedList

link.add("b");

^

LinkedListDemo.java:8: warning: [unchecked] unchecked call to add(E) as a member

of the raw type java.util.LinkedList

link.add(new Integer(10));

^

LinkedListDemo.java:12: warning: [unchecked] unchecked call to addFirst(E) as a

member of the raw type java.util.LinkedList

link.addFirst(new Integer(20));

^

LinkedListDemo.java:16: warning: [unchecked] unchecked call to addLast(E) as a m

ember of the raw type java.util.LinkedList

link.addLast("c");

^

LinkedListDemo.java:20: warning: [unchecked] unchecked call to add(int,E) as a m

ember of the raw type java.util.LinkedList

link.add(2,"j");

^

LinkedListDemo.java:24: warning: [unchecked] unchecked call to add(int,E) as a m

ember of the raw type java.util.LinkedList

link.add(1,"t");

^

7 warnings



C:\appu>java LinkedListDemo

The contents of array is[a, b, 10]

The size of the linked list is3

The contents of array is[20, a, b, 10]

The size of the linked list is4

The contents of array is[20, a, b, 10, c]

The size of the linked list is5

The contents of array is[20, a, j, b, 10, c]

The size of the linked list is6

The contents of array is[20, t, a, j, b, 10, c]

The size of the linked list is7

The contents of array is[20, t, a, b, 10, c]

The size of the linked list is6



C:\appu>


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...