Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

unknown error  RSS feed

 
Andy Rayburn
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a Lab problem for a class that I am doing but when I go to compile it I am getting an error that I have no clue what it is. Here is the program

import java.util.*;
public class Lab23A {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new String("Happy"));
list.add(new String("Sleepy"));
list.add(new String("Sneezy"));
list.add("Doc");
for (int i = 0; i < list.size(); i++) {
System.out.println((String)list.get(1));
}
}
}

The error I get is as followed

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

Can someone help please.

Thanks
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Please take time to do a quick google. Its actually quicker than posting! Anyway I guess youe are using J2SE1.5 - Google your error, and if you are still stuck look at generics

Regards

Ramen
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just about any answer to a question can be found using a search engine.

Did you recompile with -Xlint as it tells you to do? The problem does have to do with generics, but it is not an error(unless you have the compiler set to stricter then default). It is a warning, your program should run fine as is.

It is a good idea to take care of it, look up generics to fix it.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Andy, that was "only" a warning, not an error. Four warnings to be precise. At every add(). But your program compiled and will run without an exception.

Try
ArrayList<String> list = new ArrayList<String>();

instead of
ArrayList list = new ArrayList();


and perhaps work through the Sun Tutorial about Generics.


And there is one logical mistake:
System.out.println((String)list.get(1));

Shouldn't it be get(i)?


Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!