File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Prob with String arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prob with String arrays" Watch "Prob with String arrays" New topic
Author

Prob with String arrays

Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
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

Joined: Feb 27, 2002
Posts: 8919

Why will cause Exception?


Groovy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

didn't cause an exception when i ran it...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Jan 04, 2003
Posts: 867

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 ]

Francis Siu
SCJP, MCDBA
Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jan 04, 2003
Posts: 867

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

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prob with String arrays