Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning double dimension array to single dimension array of type Object

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..


 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: remember that a two-dimensional array is really an array of arrays.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys..... my doubt got cleared thankss...
 
Kushan Athukorala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate your courage

Regards,
Kushan
 
Rahul Saple
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew I was on to something. So close .Thanks.
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic