Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Java/Eclipse thinks that an ArrayList is some random Object
Java/Eclipse thinks that an ArrayList is some random Object [message #968604] Fri, 02 November 2012 15:42 Go to next message
A U is currently offline A U
Messages: 2
Registered: January 2012
Junior Member
1 System.out.println(z0.get(0)); //prints; [[OverallHeightAsString, OverallWidthAsString, OverallHeight]]
2 System.out.println(z0.get(0).getClass()); //prints; class java.util.ArrayList
3	
4 ArrayList arrayList1 = new ArrayList(z0.get(0)); 
5 System.out.println(arrayList1.get(0)); //prints; [OverallHeightAsString, OverallWidthAsString, OverallHeight]	
6 System.out.println(arrayList1.get(0).getClass()); //prints; class java.util.ArrayList
7	
8 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. Crying or Very Sad

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!! Confused
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???
Re: Java/Eclipse thinks that an ArrayList is some random Object [message #968744 is a reply to message #968604] Fri, 02 November 2012 18:11 Go to previous messageGo to next message
Eclipse User
Hi,

You are getting runtime and compile time mixed up. At compile time the
compiler has no concept of the contents. All it knows it that the
contents are some kind of Object. Since you are not using generics the
contents could be a String, Integer, ArrayList, HashMap. It has no idea.
The compiler is the one that determines what constructor to use. It
needs to put the call to that constructor in the compiled .class. Since
it doesn't know it assumes worst case, that it is an Object, and so it
can't find any such constructor.

By the way this is not eclipse jdt question. This is a basic java
question and so this should be asked on a basic java forum.

Rich
Re: Java/Eclipse thinks that an ArrayList is some random Object [message #968930 is a reply to message #968604] Fri, 02 November 2012 21:37 Go to previous messageGo to next message
Eclipse User
fwiw you are not showing us z0. its probably Arraylist<ArrayList> type.

On 11/2/2012 11:42 AM, A U wrote:
> 1 System.out.println(z0.get(0)); //prints; [[OverallHeightAsString, OverallWidthAsString, OverallHeight]]
> 2 System.out.println(z0.get(0).getClass()); //prints; class java.util.ArrayList
> 3
> 4 ArrayList arrayList1 = new ArrayList(z0.get(0)); 5 System.out.println(arrayList1.get(0)); //prints; [OverallHeightAsString, OverallWidthAsString, OverallHeight]
> 6 System.out.println(arrayList1.get(0).getClass()); //prints; class java.util.ArrayList
> 7
> 8 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. :cry:
> 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???
Re: Java/Eclipse thinks that an ArrayList is some random Object [message #973208 is a reply to message #968930] Tue, 06 November 2012 06:32 Go to previous message
Mitesh Aegis is currently offline Mitesh Aegis
Messages: 5
Registered: November 2012
Junior Member
Hi,

You do change you object name and try to run that code.

Thanks!


More info:- www.programmingpeopleinc.com
Previous Topic:Integrating with C++ backend
Next Topic:After Restart projects missing
Goto Forum:
  


Current Time: Fri Oct 24 10:32:55 GMT 2014

Powered by FUDForum. Page generated in 0.01780 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software