• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test
{
public static void main(String[] agrs)
{
int [][] a1 = { { 1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println(a1[j][i]);
}
}
}
}

Ans: at run time the values for a1 are
00 -------- 1
01 ---------2
02---------3
Like this we have to get the answer. But output for this question coming as 147258369
Can any one say why this is happening?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result u r expecting will be printed when u use a1[i][j] instead of a1[j][i]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using a1[j][i] and incrementing j in the inner loop

So :
a1[0][0] = 1
a1[1][0] = 4
a1[2][0] = 7
 
Kiran Chand Panga
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can agree for given answers but While creating the Array I didn�t mention any reference names like i or j. so why complier is assuming like first reference is �i� and second reference is �j� ?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is not assuming this
The interpreter is assuming this because this
is what you tell him to assume.

Think a bit about it.

With the code you show you are actually going through the table
[1,2,3]
[4,5,6]
[7,8,9]
in the order 1->4->7->2->5->8->3->6->9
while you want to go that way:
1->2->3->4->5->6->7->8->9

Hope this helps.
[ February 15, 2006: Message edited by: Peter Petrov ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Summary of two dimensions arrays:
[array][cells]
 
Do you pee on your compost? Does this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic