0

Hello Members,

The following code compiles and runs accurately, but gives the following message:

Note: List.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
import java.util.*;

public class List {
    private final static int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,56};

     public static void main(String args [])
     {
        ArrayList  x = new ArrayList();
         
         for (int i: array)
              x.add(i);
         
         System.out.println(x);
     }
  }

How else would I instantiate the ArrayList Object?

Thank you!

Edited by sciprog1: n/a

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by sciprog1
0

You want to give a type for that list:

ArrayList<Integer>  x = new ArrayList<Integer>();

This tells the compiler what type of object is allowed in there, which means that when you try to add, say, a JTextArea, it'll tell you "no dice". This in turn means that when you take something out of the list, you know that it's an Integer and not (for example) a JTextArea - so you can safely do Integer stuff with it.

If you search on "Java generics" you'll find a rake of information about how this works.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.