Solved

ArrayList -- Type Safety

Posted on 2006-07-02
15
2,282 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
 
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
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: 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

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
topping1 challenge 7 72
Non-recursive backtracking, using a stack 1 98
Java Options in Zimbra Server 2 44
netstat -ano | find "8000" and taskkill /f /pid 2984 3 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now