Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove type safety warning:

Posted on 2006-07-02
7
Medium Priority
?
505 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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