Solved

Remove type safety warning:

Posted on 2006-07-02
7
489 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
ejb stateless example 2 20
Adding multiple JVM environments to RedHat 6 7 47
Chrome and Firefox Java 5 50
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

839 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