Solved

Remove type safety warning:

Posted on 2006-07-02
7
481 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 22
recursion example 16 111
thymeleaf natural templating vs JSP 2 68
HSSFWorkbook cannot be resolved error 10 50
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now