java - BubbleSort StackOverflowError -


i added recursive bubblesort algorithm game runs on lwjgl. i'm trying sort arraylist of "cloud" objects float speed of cloud.

for reason "java.lang.stackoverflowerror" @ line invoke method in itself.

here's code:

public void sort() {     (int = 0; < clouds.size() - 1; i++) {         cloud cl1 = clouds.get(i);         cloud cl2 = clouds.get(i + 1);         if (cl1.getspeed() < cl2.getspeed()) {             continue;         }         clouds.set(i, cl2);         clouds.set(i+1, cl1);         this.sort();     } } 

and here errors i'm getting:

sat may 04 20:28:45 cest 2013 error:null java.lang.stackoverflowerror          @ backgrounds.clouds.sort(clouds.java:224) [...] // line above repeated hundred times. 

that happens when 2 consecutive clouds have same speed.

cl1.getspeed() < cl2.getspeed() 

is false, clouds swapped , sort called again. in call,

cl1.getspeed() < cl2.getspeed() 

is still false, swap again , call sort. goes on forever (or better: till stack full).

change < <= , should work fine.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -