File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning double dimension array to single dimension array of type Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning double dimension array to single dimension array of type Object" Watch "Assigning double dimension array to single dimension array of type Object" New topic
Author

Assigning double dimension array to single dimension array of type Object

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

public class ID
{
public static void main(String... args)
{
Object [] object = new String[5][5];
int counter=0;
for (Object o : object )
{
counter++;
}
System.out.println(counter);
}
}


this code outputs 5 ...
I am not getting why ...
can anyone explain ??? please ..



OCPJP(83%)
Kushan Athukorala
Ranch Hand

Joined: Aug 09, 2010
Posts: 33
Hi Ankur,

Can you tell us what you understand from this code first?

Then we can help you to figure out where you go wrong.

Thanks,
Kushan


Kushan Athukorala
OCPJP 6.0 [86%]
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3091
    
  33

Head First Java has beautifully explained this Array concept. I would recommend you to read that book if you have a copy of it. Its worth reading for a beginner.

Also please UseCodeTags to post your source code.


Mohamed Sanaulla | My Blog
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4489
    
    8

Hint: remember that a two-dimensional array is really an array of arrays.
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hey guys..... my doubt got cleared thankss...
Kushan Athukorala
Ranch Hand

Joined: Aug 09, 2010
Posts: 33
Appreciate your courage

Regards,
Kushan
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Could someone please tell me what exactly is going on behind the scenes. I understand that a two dimensional string array is created and the reference value of that 2-D array is given to a one dimensional Object array reference.
But how is it possible? By that rule the following line of code should compileBut it doesn't. Is it because in Java every Array is an Object? Can anyone shed more light on this?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4489
    
    8

Rahul Saple wrote:Is it because in Java every Array is an Object?



2D array = array of arrays = array of Objects
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
I knew I was on to something. So close .Thanks.
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57
int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
Object[] obj = (Object[])a.clone(); // 2
int [] obj1 = (int[])a.clone(); //3
The line 1 and 2 compiles fine. If I add line-3, it does not run. Can anyone explain me why?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning double dimension array to single dimension array of type Object