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!

Recommended Answers

All 2 Replies

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.

Hello Jon,

Thanks a lot! That was very clear.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.