Results 1 to 2 of 2
  1. #1
    beachwood23 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default ArrayList - "Unchecked or unsafe operations" at compile

    I have written the following program using an ArrayList.

    Java Code:
    import java.util.ArrayList;
     
    public class TestArrayList {
    	public static void main(String[] args) {
    		//create a list to store cities
    		ArrayList cityList = new ArrayList();
    		
    		//add some cities to the list
    		cityList.add("London");
    		//citylist now contains [London]
    		cityList.add("Denver");
    		cityList.add("Paris");
    		cityList.add("Timbuktu");
    		cityList.add("Burlington");
    		cityList.add("Dublin");
    		cityList.add("Miami");
    		cityList.add("Tokyo");
    		//cityList now contains[London,Denver,Paris,Timbuktu,Burlington, Dublin,Miami,Tokyo]
    		
    		System.out.println("list size " + cityList.size());
    		System.out.println("is miami in the list? " + cityList.contains("Miami"));
    		System.out.println("location of denver in the list " + cityList.indexOf("Denver"));
    		System.out.println("Is the list empty " + cityList.isEmpty());
    		
    		//insert new city at index 2
    		cityList.add(2, "Xian");
    		//remove a city from the list
    		cityList.remove("Miami");
    		//remove city at index 1
    		cityList.remove(1);
    		//display contents in the list
    		System.out.println(cityList.toString());
    		
    		//display contents in reverse order
    		for (int i = 0; i < cityList.size(); i++) {
    			System.out.println(cityList.get(i) + " ");
    		}
    		
    		//create a list to store two circle
    		ArrayList list = new ArrayList();
    		
    		//add two circles
    		list.add(new Circle4(2));
    		list.add(new Circle4(3));
    		
    		//display area of first circle in list
    		System.out.println("\nthe area of the circle? " + ((Circle4)list.get(0)).getArea());
    	}
    }
    When I try to compile, I have "unchecked or unsafe operations." I compiled it with the suggested -Xlint:unchecked parameter, and there are 11 errors: on all of the <ArrayList>.add(<item>).Does anyone know what I need to change, or what I'm doing wrong?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Look at the messages carefully -- they're warnings, not errors, and that's a big difference. They're warning you that you're not using generics with your ArrayList, and if you don't need to use generics, you can ignore them.

    In the future, you'll want to post your entire error messages here rather than paraphrasing them so we will have no doubt as to your exact error message.

Similar Threads

  1. Unchecked or Unsafe Operations Error Message
    By littleone in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 01:50 PM
  2. unchecked or unsafe operations-Recompile with -Xlint
    By pronetin in forum Advanced Java
    Replies: 15
    Last Post: 05-31-2010, 05:41 PM
  3. Unchecked or unsafe operations warning
    By sky in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 03:41 AM
  4. Replies: 22
    Last Post: 05-08-2008, 12:00 PM
  5. Uses unchecked or unsafe operations message
    By Robbinz in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 10:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •