Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,743 people online right now. Registration is fast and FREE... Join Now!




Compiler Warnings with lists

 
Reply to this topicStart new topic

Compiler Warnings with lists

clatcho
24 Apr, 2007 - 12:46 PM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Hi firstly the code :

CODE


   public static List asList(String aString)
   {
      List<Character> theList = new ArrayList<Character>();
      for (int x = 0; x < aString.length(); x++)
      {
         theList.add(aString.charAt(x));
      }
      return theList;
   }



This is my first class method, it takes a String as an argument, declares a list then puts the individual characters individually into the list, works as intended.

I am having problems with my second method

CODE


   public static void simpleMethod(String aString)
   {
      List<Character> newList = new ArrayList<Character>(CharacterLists.asList(aString));    
   }



This is not the complete method, just my problem, when i try to compile i get compiler warnings indicating CharacterLists.java uses unchecked or unsafe operations.

i get the same error if i were to do this :

CODE


   public static void simpleMethod(String aString)
   {
      List<Character> newList = new ArrayList();  
   }



From what i have read on the old wild web, i find that i am using a collection class without declaring what kind of stuff
it holds, taken from :

http://lists.bluej.org/pipermail/bluej-dis...rch/005433.html

I just dont get it, do you think there is something wrong with my initial method ? If so please don't give me the corrected code just a push in the right direction would be very much appreciated.
User is offlineProfile CardPM
+Quote Post

clatcho
RE: Compiler Warnings With Lists
24 Apr, 2007 - 01:10 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
I am using Blue J to write my code, as i am just learning Java, did a bit more reading, and i have switched off the :

" Show compiler warnings when unsafe collections are used "

Re-complied the code and it works fine, however it doesnt seem right that i have to do this to get the code to work . . .
User is offlineProfile CardPM
+Quote Post

keems21
RE: Compiler Warnings With Lists
24 Apr, 2007 - 05:49 PM
Post #3

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
There are two things that I see that may be causing you problems. Here's the first one:
CODE

      List<Character> theList = new ArrayList<Character>();


I know that ArrayList either extends or implements the List object, but I can't remember offhand. I would suggest creating the ArrayList like this regardless:
CODE

      ArrayList<Character> theList = new ArrayList<Character>();




The other thing that I see is in your add method:
CODE

theList.add(aString.charAt(x));


You're adding a char to the array list, but it is supposed to take a Character object. Try this instead:
CODE

theList.add(new Character(aString.charAt(x)));


Try these two things and tell me how they work.

Good luck.

Edit:
You may also be declaring your ArrayList incorrectly. Try this:
CODE

ArrayList<Character> theList = new ArrayList();


Again, I'm not sure because I'm not a big fan of ArrayLists myself.

This post has been edited by keems21: 24 Apr, 2007 - 05:51 PM
User is offlineProfile CardPM
+Quote Post

clatcho
RE: Compiler Warnings With Lists
25 Apr, 2007 - 10:31 AM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Thank you for the response,

I did what you suggested 1st with the change to the first method, the code does compile ok, although i do still get the compiler warnings,

Tried declaring the List's as suggested but that didn't work at all i'm affraid,

But not to worry i have simply swicthed off the warnings for now and can get the code to compile and it wors as expected,

Thank you for your help anyway.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month