?
Solved

Remove type safety warning:

Posted on 2006-07-02
7
Medium Priority
?
499 Views
Last Modified: 2008-02-01
The following gives a type safety warning which I am unable to lose, what is wrong?

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();
          }
     }
}

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
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17027533
> ArrayList[] Hand = new ArrayList[Cnt];
should be

ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];
0
 
LVL 8

Author Comment

by:List244
ID: 17027539
Tried that, gives:

Cannot create a generic array of ArrayList<Integer>
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17027644
May not be able to do this:
http://www.cis.upenn.edu/~matuszek/cit594-2006/Pages/coping-with-generics.html

You can try this:
                ArrayList<ArrayList<Integer>> l = new ArrayList<ArrayList<Integer>>(Cnt);
                ArrayList<Integer>[] Hand = (ArrayList<Integer>[]) l.toArray();

Why don't you use ArrayList<ArrayList<Integer>> instead of ArrayList<Integer>[]
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: 17027697
This adds the following warning:

Type safety: The cast from Object[] to ArrayList<Integer>[] is actually checking against the erased type ArrayList[]
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 17028045
Don't think you can get rid of that waring using an array (as you cannot create a array of generics), would instead need to use collection

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

Expert Comment

by:objects
ID: 17028060
your other option if you want to use array it to suppress the warning:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Random;
public class ArrayL
{
      @SuppressWarnings("unchecked")
     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);
               }
          }
          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 8

Author Comment

by:List244
ID: 17028118
Thanks, that worked for me.
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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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