Click to See Complete Forum and Search --> : Linked List


coder752
July 29th, 2009, 08:44 PM
How can I compile this code found on this site?

http://www.javafaq.nu/java-example-code-90.html

The code is too long to paste here.

It's a linked list example.

No, its not a dumb question because I know normally you use javac (the file).java

But for this example at this site, something technical is going on.

So thanks,

Coder752

dlorde
July 30th, 2009, 05:12 AM
But for this example at this site, something technical is going on.:confused: it's code - it's expected to be technical - unless 'technical' has some new meaning I'm not familiar with...

So what happens when you try to compile it?

Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise...
E. Dijkstra

Xeel
July 30th, 2009, 12:07 PM
Emmm... you just compile it and run it... as any other java class. The class has no errors and compiles perfectly. I've just tried it.

Ye, it's not a dumb question, it's "how to compile a java class" question. Here's your answer:
- http://v1.dione.zcu.cz/java/docs/jdk1.1/docs/tooldocs/win32/javac.html (basic)
- http://ptolemy.eecs.berkeley.edu/~johnr/tutorials/tcljava98/notes/compiling.html (alternative)
- http://www.iam.ubc.ca/guides/javatut99/native1.1/stepbystep/ (native method support)

A common error is wrong classpath settings for javac. Double-check them and it WILL work...

coder752
July 30th, 2009, 12:14 PM
Not uh!

It's not as easy as pie.

In case you didn't try to compile it urselves.

This is what I get.

Note: LinkedListExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Martin O
July 30th, 2009, 01:08 PM
Not uh!
Huh?

In case you didn't try to compile it urselves.
Xeel said he just tried it!


This is what I get.
Note: LinkedListExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

:rolleyes:

http://lmgtfy.com/?q=uses+unchecked+or+unsafe+operations

Xeel
July 30th, 2009, 01:45 PM
LinkedListExample.java uses unchecked or unsafe operations

The reason for that is that you use Java v1.5+ compiler and that the class uses generics in this example. To the same List object String, Integer and ListIteratior objects are being added there. So it's impossible to define one data type for the List.

You can set a generic data type for every declaration and constructor, or just add @SuppressWarnings("unchecked") line before doLinkedListExample() method declaration so the compiler ignore the warnings.

dlorde
July 30th, 2009, 02:35 PM
A Google for "uses unchecked or unsafe operations" gives plenty of explanations of that warning. The OP seems unable to do a simple web search :rolleyes:

One can think effectively only when one is willing to endure suspense and to undergo the trouble of searching...
J. Dewey

Xeel
July 30th, 2009, 03:50 PM
A Google for "uses unchecked or unsafe operations" gives plenty of explanations of that warning. The OP seems unable to do a simple web searchI always thought it's more time consuming to type the posts on a forum and check answers later... Well, this is one of the miracles of imperfect human psychology... It's easier to ask how to make a cofee here than to search for the info even if the answer will be behind the very first link.

coder752
July 30th, 2009, 06:20 PM
So I put that line above that, and I get 2 errors. Now what?

Here's the new main.



/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
LinkedListExample listExample = new LinkedListExample();
@SuppressWarnings("unchecked") //the new line I added as suggested above.
listExample.doLinkedListExample();
}

}


It now says <identifier expected>
listExample.doLinkedListExample();


and <identifier expected>
listExample.doLinkedListExample();

jcaccia
July 30th, 2009, 06:48 PM
@SuppressWarnings("unchecked") goes just before public void doLinkedListExample(), not when it is used.

Another way to avoid the warnings (without suppressing them) would be to declare the lists as List<Object> (to allow the addition of Integers and Strings to the same list), but then you would get an error with Collection.sort. If, instead of <Object> you use <String> (you'll also need to convert the integers to String) you'll have no problems with sort.

Xeel
July 30th, 2009, 06:54 PM
So I put that line above that, and I get 2 errors. Now what?Do you understand what "method declaration" means?

Another way to avoid the warnings (without suppressing them) would be to declare the lists as List<Object> (to allow the addition of Integers and Strings to the same list), but then you would get an error with Collection.sort. If, instead of <Object> you use <String> (you'll also need to convert the integers to String) you'll have no problems with sort. I've tried this already, can be solved with generic types. It's much easier just to suppress the warnings. In classes like this a programmer usually knows what he/she is doing so there is no real need for types definition anyways, same for casts.

dlorde
July 31st, 2009, 04:20 AM
Again, if the OP had Googled @SuppressWarnings("unchecked"), the first link returned is to the Java Tutorial on Annotations, complete with full description and examples of use of that specific annotation... :rolleyes:

Surely this is the minimum amount of effort to expect?

I cannot teach anybody anything, I can only make them think...
Socrates

coder752
July 31st, 2009, 09:55 AM
@SuppressWarnings("unchecked") goes just before public void doLinkedListExample(), not when it is used.

Another way to avoid the warnings (without suppressing them) would be to declare the lists as List<Object> (to allow the addition of Integers and Strings to the same list), but then you would get an error with Collection.sort. If, instead of <Object> you use <String> (you'll also need to convert the integers to String) you'll have no problems with sort.

Thanks! I just put that line in its new location, it works now.

I don't think I could do the other method Xeel said earlier since I didn't learn generics yet.