2

I ask that I do not get direct answers, but rather hints to help me continue learning.

My task is to create an array > populate it > sort it > print unsorted and sorted.

I either end up with an arrayList that is full of the lowest number, or I run into the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

for (int index = 0; index <= 29; index ++){

    System.out.println(" I have reached the first ForLoop Sort " + index);

    for (int i = 0 ; i <= midSortArray.size(); i++){

        System.out.println(" I have reached the Second ForLoop Sort " + i);

        if(currentInteger <= midSortArray.get(i)){

            System.out.println(" If is true ");
            currentInteger = midSortArray.get(i);

        } else {

            System.out.println("If is false");
            lowestInteger = currentInteger;
            sortedArray.add(0, lowestInteger);
            midSortArray.remove(midSortArray.indexOf(lowestInteger));
            System.out.println(sortedArray);

        }

    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    When `i == midSortArray.size()`, you're out of the bounds of the list. You only want to continue while `i < midSortArray.size()`. – shmosel Oct 05 '16 at 23:01
  • 1
    What is `index`? `29` seems like a very random number. The inner loop will run `29 * midSortArray.size()` times – OneCricketeer Oct 05 '16 at 23:04
  • 1
    This small snipplet leaves me with various questions. I don't see `currentInteger` initialized any where so I'm not sure how it will behave on the first iteration. Second, if your array was already in a sorted manner, the if would always be true (i presume), but I don't see where your ever moving that to the sorted list. You only do so if you find something larger. I have concerns about the else too, but without knowing how the `currentInteger` starts out I can't really describe them accurately. – Taplar Oct 05 '16 at 23:16
  • 1
    I guess it feels like the else logic is moving over 'smaller' values each time, not necessarily the 'smallest'. – Taplar Oct 05 '16 at 23:17
  • Sorry, I didn't know if I should post my entire code or not, so there are a lot of things that seem strange. currentInteger is intialized elsewhere. I also set the index to 29 because earlier in my code I populated the unsortedArray with 30 random numbers. – Ryan Starno Oct 06 '16 at 03:27

0 Answers0