Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi..
The string is
String [][] array1 = new String[][]{{"aaaa", "bbbb"},{"aaaa"," cccc"," dddd"," eeee"},{"aaaaa", "fffff", "gggg"}};


I want to display the elements as
aaaa, bbbb
aaaa,cccc
aaaa,dddd
aaaa,eeee
aaaaa,fffff
aaaaa,gggg

The following code
C#
for (int row1=0;row1<=array1.length;row1++){
           for  ( int cols=0 ;cols<array1[row1].length ;cols++){
                      System.out.println(array1[0][0] +" "+ array1[row1][cols+1]);
}

}
gives me output as aaaa bbbb
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

Please suggest me what I should do to get the prefered output..Any help will be appreciated ...
Posted
Updated 7-Feb-13 22:52pm
v2
Comments
Orcun Iyigun 8-Feb-13 4:26am    
I think your array specification is wrong for a multidimensional array... Don't you get an error on it? The elements seems ambiguous..
I think example usage should be:
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };

1 solution

You are stepping outside of the array, change it to this;

Java
for (int row1=0;row1<array1.length;row1++) {
   for (int cols=1 ;cols<array1[row1].length ;cols++) {
      System.out.println(array1[0][0] +" "+ array1[row1][cols]);
   }
}



/Fredrik
 
Share this answer
 
v2
Comments
pari2 8-Feb-13 7:09am    
thank you Fredrik Bornander..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900