Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayOutOfBounds why?

 
K. Tsang
Bartender
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic