Solved

ArrayList -- Type Safety

Posted on 2006-07-02
15
2,290 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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 53
ejb example issues 3 28
What's wrong with this code? 4 26
Problem to Alipay 10 46
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

828 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