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 ArrayOutOfBounds why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayOutOfBounds why?" Watch "ArrayOutOfBounds why?" New topic
Author

ArrayOutOfBounds why?

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

OK I have a unexpected ArrayOutOfBounds error looping to arraylist.
I have 2 arraylists ccyAry and amtCcyList. ccyAry contains selected elements (from checkboxes). amtCcyList contains the elements from database.

Say ccyAry has 1 element and amtCcyList has 1 - no problem. When ccyAry has 2 element and amtCcyList has 1 - ArrayOutOfBounds?

<blockquote>code:
<pre name="code" class="core">
int k=0;
for(int i=0; i < ccyAry.size(); i++){
System.out.println("k="+k+" i="+i);
System.out.println(amtCcyList.get(k) + "=" + ccyAry.get(i)+"?");
if(amtCcyList.get(k).equals(ccyAry.get(i))){
System.out.println(amtCcyList.get(k) + "=" + ccyAry.get(i));
k++;
System.out.println("k incremented" + k);
}
else
{
System.out.println(amtCcyList.get(k) + "!=" + ccyAry.get(i));
}
</pre>
</blockquote>

The code above tests if the elements are equal, if so increment counter k, which gets the next element for amtCcyList and continue checking.
Eg ccyAry = {"CNY","HKD"};
amtCcyList = {"HKD"};

Output
k=0 i=0
HKD=CNY?
HKD!=CNY
k=0 i=1
HKD=HKD?
//program prompts error expect output prints HKD=HKD

Now I don't understand why when k=0,i=1 will produce ArrayOutOfBounds error since according to my arraylists it's doing "HKD".equals("HKD") which is legitimate. Since the get() can indeed get the elements from the arraylist, why equals prompts error?

Thanks
[ July 16, 2008: Message edited by: K. Tsang ]

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

it would help if you posted your actual code that is showing the problem. When i did what I thought was the minimum i needed to get it to compile, it ran without error, and I got the following output:

k=0 i=0
HKD=CNY?
HKD!=CNY
k=0 i=1
HKD=HKD?
HKD=HKD
k incremented1

No errors at all.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

OK since I'm working with checkboxes and stuff, the program is really for web apps. The page that calls this below "my problematic code" is a update form and when user clicks some checkbox calls AJAX to display the below according to the list of currencies previously selected. But the idea is the same - once condition met display a table.

Code deleted
[ July 17, 2008: Message edited by: K. Tsang ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

do you really need all this code to demonstrate your problem? i was able to do it in 28 lines, total:
<blockquote>code:
<pre name="code" class="core">import java.util.ArrayList;
public class Slop {
public static void main(String [] args)
{
ArrayList ccyAry = new ArrayList();
ccyAry.add("CNY");
ccyAry.add("HKD");
ArrayList amtCcyList = new ArrayList();
amtCcyList.add("HKD");

int k=0;
for(int i=0; i < ccyAry.size(); i++)
{
System.out.println("k="+k+" i="+i);
System.out.println(amtCcyList.get(k) + "=" + ccyAry.get(i)+"?");
if(amtCcyList.get(k).equals(ccyAry.get(i)))
{
System.out.println(amtCcyList.get(k) + "=" + ccyAry.get(i));
k++;
System.out.println("k incremented" + k);
}
else
{
System.out.println(amtCcyList.get(k) + "!=" + ccyAry.get(i));
}
}
}
}
</pre>
</blockquote>
This sets up the situation exactly as you describe, as far as i can tell. it works. So, either your code is different somewhere, or the situation is not as you describe.

you're gonna find very few folks who are going to invest time into looking at your hundreds of lines of code. can you simplify the problem at all?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

I found the problem with my code. The code I demonstrated indeed works fine as is. The problem was not with the "equals" method I thought earlier but was something else (db object) that I used the wrong index.

I also think my current approach is kind of clumsy/messy so I shall rethink the implementation/logic.

Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayOutOfBounds why?