Results 1 to 3 of 3
  1. #1
    aanuit is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Java/Eclipse thinks that an ArrayList is some random Object

    Java Code:
    System.out.println(z0.get(0)); //prints; [[OverallHeightAsString, OverallWidthAsString, OverallHeight]]
    System.out.println(z0.get(0).getClass()); //prints; class java.util.ArrayList
    	
    ArrayList arrayList1 = new ArrayList(z0.get(0)); 
    System.out.println(arrayList1.get(0)); //prints; [OverallHeightAsString, OverallWidthAsString, OverallHeight]	
    System.out.println(arrayList1.get(0).getClass()); //prints; class java.util.ArrayList
    	
    ArrayList arrayList2 = new ArrayList(arrayList1.get(0)); //gives error; The constructor ArrayList(Object) is undefined

    I can not run the code above, because line 8 gives an error.

    Please have a look at line 6.
    The class of "arrayList1.get(0)" is java.util.ArrayList.

    Now have a look at line 8.
    I want to create a new ArrayList based on an existing ArrayList. Line 8 returns an error.
    I think it says that "arrayList1.get(0)" an Object and that it is not possible to create a new arraylist this way.

    This is strange!!
    In line 6 "arrayList1.get(0)" is a java.util.ArrayList.
    In line 8 "arrayList1.get(0)" is an Object.

    Do you think this is strange or am I missing something???

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Java/Eclipse thinks that an ArrayList is some random Object

    You are missing something :)

    The compiler can`t know what`s in your lists if you did not explicitly "say".
    You have to use generics or you have to cast arrayList1.get(0) in line 8 explicity to ArrayList! (the first one would be better :D)

    The compiler thinks you have lists like ArrayList<Object> (note that arraylists themselves are objects too!), so at arrayList1.get(0) will return an object from type "Object" --> so you will get this error! So its the same like
    Java Code:
    	
    Object obj1 = new ArrayList();
    ArrayList list = new ArrayList(obj1); //error
    http://docs.oracle.com/javase/tutorial/java/generics/

  3. #3
    aanuit is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java/Eclipse thinks that an ArrayList is some random Object

    Thank you!!

Similar Threads

  1. extracting 5 random elements from an ArrayList
    By ankit1801 in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 06:17 AM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. How To Move An Object On A Random Path
    By burakesme in forum Java 2D
    Replies: 1
    Last Post: 05-23-2010, 05:22 PM
  4. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  5. Replies: 1
    Last Post: 03-04-2009, 06:14 PM

Posting Permissions

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