0

I am trying to use the summer to practice more Java to get better by learning how to code algorithms. I have this problem where I add elements to my ArrayList but somehow the first number I add also sets the number of positions in my list which I want to avoid. I only want the 0th index to contain the number 5. I seem to not catch a clue on how to solve this.

public class Algorithms {

private ArrayList<Integer> numbers;

public Algorithms() {

    numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(4);
    bubblesort();
}

public static void main(String args[]) {
    new Algorithms();
}

public void bubblesort() {

    System.out.println(numbers);
    for (int a = 0; a < numbers.size();) {
        for (int b = 1; b < numbers.size();) {
            int currentNumber = numbers.get(a);
            if (currentNumber > numbers.get(b)) {

                //Collections.swap(numbers, currentNumber, numbers.get(b));

                numbers.set(numbers.get(a), numbers.get(b));
                numbers.set(numbers.get(b), numbers.get(a));

                a++;
                b++;

            } else if (currentNumber < numbers.get(b)) {
                a++;
                b++;
            }
            System.out.println(numbers);
        }
    }
}
}
1
  • What do you think numbers.set(numbers.get(a), numbers.get(b)); should be doing? Did you take a look at the ArrayList API? Commented Sep 1, 2013 at 18:42

2 Answers 2

3

You are not swapping elements correctly. Instead of

numbers.set(numbers.get(a), numbers.get(b));
numbers.set(numbers.get(b), numbers.get(a));

it should be

int temp = numbers.get(a);
numbers.set(a, numbers.get(b));
numbers.set(b, temp);
Sign up to request clarification or add additional context in comments.

1 Comment

Right so that's now all working, however when I choose to add more elements to the list it sorts i, but at the expense of an indexoutofBounds Exception I know that it's to do with the temporary variable int currentNumber = numbers.get(a); pointing to the next index of the list which doesn't exist. I can't seem to find a way how to get around that problem if you could please help me find a way around it.
1

The below two statements:

numbers.set(numbers.get(a), numbers.get(b));
numbers.set(numbers.get(b), numbers.get(a));

is not performing swapping. The first argument to the List#set(int, E) method is the index in the list, where you want to set the value passed as 2nd argument. You need to use a temp variable for swapping.

Also, the swapping didn't work for your commented line for the same reason. Collections#swap method take indices for swapping. So, just change:

Collections.swap(numbers, currentNumber, numbers.get(b));

to:

Collections.swap(numbers, a, b);

And please for the love of all that is holy, don't call method from inside a constructor. Remove the method invocation from inside the constructor, and move it to main method like this:

Algorithms algo = new Algorithms();
algo.bubbleSort()

1 Comment

Oh so that's the way I can call methods directly from the main method. Cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.