• Post Reply Bookmark Topic Watch Topic
  • New Topic

displaying 2 dim array  RSS feed

 
purna adinarayan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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]);

}

}

%>
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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"};
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!