What is wrong in this code?

Hello,

I have a ArrayList called "Storage" which contains String elements and two other ArrayLists , "DrawPointsX and DrawPointsY" which contain integer elements. All the ArrayList are of the same size and each element has a corresponding drawing points in both DrawPointsX and DrawPointsY. Eg, String element "Josy Wales" with index - Storage.get(b)[c] will have corresponding x-axis point in DrawPointsX.get(b)[c] and the same for DrawPointsY.

I planned to implement a method which checks when a string element first occur in the ArrayList "Storage" and how long it takes to occur again.

If it occur the first time and takes time longer than a given threshold to occur again, then I will delete in the "Storage" where ever this element is found and also delete their corresponding index in DrawPointsX and DrawPointsY.

If it occurs just once and if after a given time threshold (iteration) it does not occur, the element will be deleted with the corresponding drawing points.

Below is my method, which gives an IndexOutOfBoundsException. My question is, is anything wrong in this code?

publicvoid DrawPoints(){

System.out.println("DrawPoints()");

for(int a = 0; a < Storage.size(); a++){

for(int b = 0; b < Storage.get(a).length; b++)

for(int c = a+1; c < Storage.size(); c++){

for (int d = 0; d < Storage.get(c).length; d++)

if(Storage.get(a)[b].compareTo(Storage.get(c)[d])== 0){

if(Math.abs(c - a) > threshold){

//remove the element where ever it is found in the "Storage" and the Drawing Points in DrawPointsX and DrawPointsY

for(int r = 0; r < Storage.size(); r++){

for (int s = 0; s < Storage.get(r).length; s++){

if(Storage.get(r)[s].compareTo(Storage.get(a)[b]) == 0){

Storage.remove(Storage.get(r)[s]);

DrawPointsX.remove(DrawPointsX.get(r)[s]);

DrawPointsY.remove(DrawPointsY.get(r)[s]);

}

}

}

}

}

else

if (Storage.get(a)[b].compareTo(Storage.get(c)[d])!= 0){

//counter is the same as the size of the ArrayList

if(Math.abs(counter - a ) > threshold){

Storage.remove(Storage.get(a)[b]);

DrawPointsX.remove(DrawPointsX.get(a)[b]);

DrawPointsY.remove(DrawPointsY.get(a)[b]);

}

}

}

}

}

Thanks,

Jona_T

[3671 byte] By [Jona_Ta] at [2007-11-27 11:47:30]
# 1

What is wrong with this code is there is too much nesting.

7 for loops and 5 if statements is too much to put all in one method like that. Refactor this code into smaller pieces so that the logic is less convoluted.

cotton.ma at 2007-7-29 18:13:41 > top of Java-index,Java Essentials,New To Java...
# 2

> I have a ArrayList called "Storage" which contains String elements

> and two other ArrayLists , "DrawPointsX and DrawPointsY" which

> contain integer elements. All the ArrayList are of the same size and

> each element has a corresponding drawing points in both

> DrawPointsX and DrawPointsY. Eg, String element "Josy Wales" with

> index - Storage.get(b)[c] will have corresponding x-axis point in

> DrawPointsX.get(b)[c] and the same for DrawPointsY.

This should not be implemented as 3 ArrayLists. This should be a single class that holds a String and two ints. Then you should have a single generic arraylist of that only holds objects of the class: ArrayList<MyClass>. This can simplify things immensely.

Also, is Storage (bad name, it should be storage) an ArrayList of String or an ArrayList of an array of strings?

In short, I think that you have to seriously rethink your basic data design.

Good luck!

petes1234a at 2007-7-29 18:13:41 > top of Java-index,Java Essentials,New To Java...
# 3

Hello,

Thanks for the advice. I got it working after re-structuring the code.

Jona_T

Jona_Ta at 2007-7-29 18:13:41 > top of Java-index,Java Essentials,New To Java...