• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

displaying 2 dim array

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i am trying to display the contents of this array its showing arrayindexoutofbounds exception...how do i display this array?




<%

String names[][] = new String[10][2];



for(int i=0;i>=10;i++){

for(int j=0;j<=i;j++){


names[i][j]="anu";

if(j>0){

names[i][j]="radha";

}
}
}


for(int z=0;z<10;z++){


for(int y=0;y<z;y++){

System.out.println(names[z][y]);

}

}

%>
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first set of loops doesn't actually do anything -- as the outer loop doesn't run any iterations. This means that you never set any strings, and hence, there is nothing to display.

The second set of loops ... well, the shape of the arrays is like a "10 by 2 rectangle" and you traverse it like a "10 by 10 right triangle". This is, of course, where your array out of bounds is happening.

Henry
 
purna adinarayan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:The first set of loops doesn't actually do anything -- as the outer loop doesn't run any iterations. This means that you never set any strings, and hence, there is nothing to display.

The second set of loops ... well, the shape of the arrays is like a "10 by 2 rectangle" and you traverse it like a "10 by 10 right triangle". This is, of course, where your array out of bounds is happening.

Henry



i changed the code..i am now getting only null values....please help me with this.....




i want the output to be displayed like this as below:


anu radha
anu radha
anu radha
.
.
.
.
.
10 times
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The first loop still doesn't do anything -- so nothing is actually set.

Henry
 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

purna adinarayan wrote:




Just work through this code fragment on paper and see if it's actually doing what you want it to do!
Hint: Pay attention to the loop condition in the for loop.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct way to display that array is of course
System.out.println(Arrays.deepToString(myArray));

Start by stopping saying 2D array. You have an array of arrays, which is different from the old concept of a 2D array. Whenever you iterate that array, you get its elements which are arrays. So you have to iterate those arrays too.

Of course you can always assign a complete array
names[0] = new String[]{"Anu", "Radha"};
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic