Solved

ArrayList -- Type Safety

Posted on 2006-07-02
15
2,287 Views
Last Modified: 2008-01-09
Hi, I am trying to do a simple array list and have run into a problem, any help would be great:

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++)
            {
                  for(int a =0;a<3;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
  • 7
  • 6
  • 2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026899
That's a warning. You are compiling with a Java 1.5 compiler. To remove the warning do

javac -source 1.4 ArrayL.java
0
 
LVL 8

Author Comment

by:List244
ID: 17026922
I am using Eclipse, what do you mean by do:
javac -source 1.4 ArrayL.java


Also, is there no way then to solve the problem via code?  Why does it give me that warning?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 17026934
>>I am using Eclipse,

Then you could set Eclipse so that a target of 1.4 is specified but

>>Also, is there no way then to solve the problem via code?

Yes,  you would do

ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

and

Iterator<Integer> T = Hand[i].iterator();
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Author Comment

by:List244
ID: 17026941
Is there somewhere I can go to learn what to put in the <> for each data-type?
For example, what do I use for long? char? double? float? String?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026954
You would use the wrapper class for each

Long, Character, Double, Float, String

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
0
 
LVL 8

Author Comment

by:List244
ID: 17026960
Alright, thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026967
:-)
0
 
LVL 8

Author Comment

by:List244
ID: 17026971
Hmm, wait, now I get this:
Cannot create a generic array of ArrayList<Integer>

ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

Why is that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17026994
                 ArrayList[] Hand = new ArrayList[3];
                  Hand[0] = new ArrayList<Integer>();
0
 
LVL 8

Author Comment

by:List244
ID: 17027035
Okay, I have this, but it is still giving me the warning of Type Safety:

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

Expert Comment

by:CEHJ
ID: 17027673
That is probably because of

>>Iterator T = Hand[i].iterator();

See my  earlier posting for the correct version
0
 
LVL 8

Author Comment

by:List244
ID: 17027692
The iterator change actually adds:

Type safety: The expression of type Iterator needs unchecked conversion to conform to Iterator<Integer>      
0
 
LVL 92

Expert Comment

by:objects
ID: 17028110
>>Also, is there no way then to solve the problem via code?
> Yes,  you would do
> ArrayList<Integer>[] Hand = new ArrayList<Integer>[Cnt];

Not sure whu you accepted that becuase it is incorrect.
0
 
LVL 92

Expert Comment

by:objects
ID: 17028114
You need to either use a List instead of an array.
Or suppress checked warnings.
0
 
LVL 8

Author Comment

by:List244
ID: 17028115
Objects, I know, I didn't mean to accept it.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
map related example 6 48
How to execute a Python program and gather return output in Java 2 29
Running JavaFX on JDeveloper 12C 1 32
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…
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.

770 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