"unsafe and unchecked" error

J

justineee

Hi Everyone,

I got this code from a site and when I compiled...

Note: C:\Users\Acer M5640\Documents\2nd term\DASTRAP\Sample.java uses
unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

this error occured.. I really need this for my project in school can
anyone give me some tips?

import java.awt.*;
import java.text.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

public class Sample {
public static void main(String args[]) {
Runnable runner = new Runnable()
{
public void run()
{ String[] array=new String[5];
Scanner sc=new Scanner(System.in);
int ctr;
for (ctr = 0; ctr < array.length; ctr++)
{
System.out.print("ENTER WORD " + (ctr+1) + ": ");
array[ctr]=sc.next();
}


List list = Arrays.asList(array);
Collections.sort(list);
System.out.println(list.toString ());

}
};
EventQueue.invokeLater(runner);
}
}
 
J

Joshua Cranmer

justineee said:
Hi Everyone,

I got this code from a site and when I compiled...

Note: C:\Users\Acer M5640\Documents\2nd term\DASTRAP\Sample.java uses
unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

this error occured.. I really need this for my project in school can
anyone give me some tips?

List list = Arrays.asList(array);

A list of what?
<http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html>
 
H

Harold Yarmouth

justineee said:
Hi Everyone,

I got this code from a site and when I compiled...

Note: C:\Users\Acer M5640\Documents\2nd term\DASTRAP\Sample.java uses
unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

this error occured.. I really need this for my project in school can
anyone give me some tips?

It's not an error, just a warning (I think). What the compiler finds
disturbing is this bit:
List list = Arrays.asList(array);
Collections.sort(list);
System.out.println(list.toString ());

These days we have these nifty things called generics, which means you
can declare the list as a List<String>.

Since all this does is sort the contents and then dump the list with
toString, and since the contents are Comparable, it is actually
perfectly type-safe anyway (try running it, it should actually already
be working, unless you have a bug elsewhere). The compiler just couldn't
prove it, and therefore didn't like it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top