Solved

Remove type safety warning:

Posted on 2006-07-02
7
479 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

760 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

18 Experts available now in Live!

Get 1:1 Help Now