?
Solved

ArrayList -- Type Safety

Posted on 2006-07-02
15
Medium Priority
?
2,309 Views
Last Modified: 2008-01-09
Hi, I am trying to do a simple array list and have run into a problem, any help would be great:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Random;
public class ArrayL
{
      public static void main(String[] args)
      {
            Scanner Input = new Scanner(System.in);
            int Cnt = Input.nextInt();
            ArrayList[] Hand = new ArrayList[Cnt];
            for(int i=0;i<Cnt;i++)
            {
                  for(int a =0;a<3;a++)
                  {
                        Random Num = new Random();
                        int NumC= Num.nextInt()%4;
                        Hand[i].add(NumC); //////////////WARNING
                  }
            }
            for(int i=0;i<Cnt;i++)
            {
                  Iterator T = Hand[i].iterator();
                  while (T.hasNext())
                  {
                        System.out.print(T.next() + ",");
                  }
                  System.out.println();
            }
      }
}

Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList<E> should be parameterized
0
Comment
Question by:List244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026899
That's a warning. You are compiling with a Java 1.5 compiler. To remove the warning do

javac -source 1.4 ArrayL.java
0
 
LVL 8

Author Comment

by:List244
ID: 17026922
I am using Eclipse, what do you mean by do:
javac -source 1.4 ArrayL.java


Also, is there no way then to solve the problem via code?  Why does it give me that warning?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 17026934
>>I am using Eclipse,

Then you could set Eclipse so that a target of 1.4 is specified but

>>Also, is there no way then to solve the problem via code?

Yes,  you would do

ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

and

Iterator<Integer> T = Hand[i].iterator();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Author Comment

by:List244
ID: 17026941
Is there somewhere I can go to learn what to put in the <> for each data-type?
For example, what do I use for long? char? double? float? String?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026954
You would use the wrapper class for each

Long, Character, Double, Float, String

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
0
 
LVL 8

Author Comment

by:List244
ID: 17026960
Alright, thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026967
:-)
0
 
LVL 8

Author Comment

by:List244
ID: 17026971
Hmm, wait, now I get this:
Cannot create a generic array of ArrayList<Integer>

ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

Why is that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026994
                 ArrayList[] Hand = new ArrayList[3];
                  Hand[0] = new ArrayList<Integer>();
0
 
LVL 8

Author Comment

by:List244
ID: 17027035
Okay, I have this, but it is still giving me the warning of Type Safety:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Random;
public class ArrayL
{
      public static void main(String[] args)
      {
       
            Scanner Input = new Scanner(System.in);
            int Cnt = Input.nextInt();
            ArrayList[] Hand = new ArrayList[Cnt];
            for(int i=0;i<Cnt;i++)
            {
              Hand[i] = new ArrayList<Integer>();
                  for(int a =0;a<5;a++)
                  {
                        Random Num = new Random();
                        int NumC= Num.nextInt()%4;
                        Hand[i].add(NumC);        ///////////WARNING
                  }
            }
            for(int i=0;i<Cnt;i++)
            {
                  Iterator T = Hand[i].iterator();
                  while (T.hasNext())
                  {
                        System.out.print(T.next() + ",");
                  }
                  System.out.println();
            }
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17027673
That is probably because of

>>Iterator T = Hand[i].iterator();

See my  earlier posting for the correct version
0
 
LVL 8

Author Comment

by:List244
ID: 17027692
The iterator change actually adds:

Type safety: The expression of type Iterator needs unchecked conversion to conform to Iterator<Integer>      
0
 
LVL 92

Expert Comment

by:objects
ID: 17028110
>>Also, is there no way then to solve the problem via code?
> Yes,  you would do
> ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

Not sure whu you accepted that becuase it is incorrect.
0
 
LVL 92

Expert Comment

by:objects
ID: 17028114
You need to either use a List instead of an array.
Or suppress checked warnings.
0
 
LVL 8

Author Comment

by:List244
ID: 17028115
Objects, I know, I didn't mean to accept it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question