0

I'm debugging a small program in java, a strange error occurred:

import java.util.*;
public class DebugNine3
{
   public static void main(String[] args)
   {
      ArrayList products = new ArrayList(3);
      products.add("shampoo");
      products.add("moisturizer");
      products.add("conditioner");
      Collections.sort(products);
      display(products);
      final String QUIT = "quit";
      String entry;
      Scanner input = new Scanner(System.in);
      System.out.print("\nEnter a product or " + QUIT + " to quit >> ");
      entry = input.nextLine();
      while(!entry.equals("quit"))
      {
         products.add(entry);
         Collections.sort(products);
         display(products);;
      }
   }

   public static void display(ArrayList products)
   {
      System.out.println("\nThe size of the list is " + products.size());
      for(int x = 0; x <= products.size(); ++x)
         System.out.println(products.get(x));
   }
}

Note: DebugNine3.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

can someone explain why this message appeared?

paradox
  • 259
  • 3
  • 15
  • Perhaps you should use ArrayList not just ArrayList – asaini007 Oct 30 '13 at 19:27
  • [This](http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning) question is similar to yours and there is a good answer. – Krayo Oct 30 '13 at 19:30

2 Answers2

1

you are using an ArrayList that does not have a type.

ArrayList<String> products = new ArrayList<String>(3);

Should fix your problem.

Todoy
  • 935
  • 8
  • 16
0

your ArrayList products = new ArrayList(3); does not use Generics so the compiler is telling you that it can't guarentee that you won't put the wrong type in at runtime.

You should change that to ArrayList<String> products = new ArrayList<String>(3);

See Java's tutorial for more information: http://docs.oracle.com/javase/tutorial/java/generics/why.html

dkatzel
  • 29,286
  • 2
  • 57
  • 64