Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prob with String arrays

 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
check out this code.
public class Mickle
{
public static void main(String argv[])
{
new Mickle();
}
public Mickle()
{
String s[][] = new String[][]{{"a","b"},{"aa","bb"} };
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++)
{
System.out.println(s[i][j]);
}
}
}

will cause an ArrayIndexoutofBound Exception.
Know why?
regards,
Mahesh
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why will cause Exception?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't cause an exception when i ran it...
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked fine when I ran it as well. By the way to keep you formatting when posting code, encase your code in '' tags
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It works fine and if you change i<=2 or j<=2 it will cause an ArrayIndexoutofBound Exception.
Mahesh Pinnamaneni
Did you try it?

[ September 29, 2004: Message edited by: siu chung man ]
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys,
here is the actual code:
---code---
String s1[][]= new String[2][2];
System.out.println(s1[1][2]);
---end---
which cause error.
regards,
mahesh
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1[][]= new String[2][2];
System.out.println(s1[1][2]);


This is cause ArrayIndexOfBoundsException because you are trying to access element [1][2] which does exist. When you declare an array of size n , the valid indices are 0 to n-1, where n >0. The indices begin from 0 and not 1 as in PASCAL.


String s1[][]= new String[2][2];
System.out.println(s1[0][1]);
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You created a 2X2 string and the program is correct if and only if the index starts from 1, however Index starts from 0, so the maximum of s1 is s1[1][1].
HTH
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition, I'm listing the valid elements in the matrix that mahesh declared above as String s1[][]= new String[2][2];

The valid elements that be used in the application are:
s1[0][0]
s1[0][1]
s1[1][0]
s1[1][1]

Any others are invalid and will cause ArrayIndexOfBoundsException... Hope it helps...
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Pinnamaneni:
Guys,
check out this code.
public class Mickle
{
public static void main(String argv[])
{
new Mickle();
}
public Mickle()
{
String s[][] = new String[][]{{"a","b"},{"aa","bb"} };
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++)
{
System.out.println(s[i][j]);
}
}
}




will cause an ArrayIndexoutofBound Exception.
Know why?
regards,
Mahesh



I compiled your code, it work fine.

But you can chage code follow below code.




New code should check null before access your array.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Pinnamaneni:
sorry guys,
here is the actual code:
---code---
String s1[][]= new String[2][2];
System.out.println(s1[1][2]);
---end---
which cause error.
regards,
mahesh


index of array in Java start with zero (0).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic