Solved

ArrayList -- Type Safety

Posted on 2006-07-02
15
2,270 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
Alright, thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
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
Comment Utility
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
Comment Utility
                 ArrayList[] Hand = new ArrayList[3];
                  Hand[0] = new ArrayList<Integer>();
0
 
LVL 8

Author Comment

by:List244
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
You need to either use a List instead of an array.
Or suppress checked warnings.
0
 
LVL 8

Author Comment

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

Featured Post

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

Join & Write a Comment

Suggested Solutions

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…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

13 Experts available now in Live!

Get 1:1 Help Now