-4

I'm making a program that arranges an array by moving the largest value to the end but for some reason, the program isn't arranging the values menually. How can I fix that?

public static void main(String args[]) {
    ArrayList<Integer> list = new ArrayList<>(); 
    list.add(15); 
    list.add(78); 
    list.add(44); 
    list.add(99); 
    list.add(32);
    
    int num = 1;
    for(int i = 0; i<=list.size() -1;){
        if(list.get(i) > list.get(i+1)){
            list.add(list.get(i));
            list.remove(i);
            System.out.println(list);
        }
        else{
            i+=1;
            num+=1;
        }
    }
}

and this what I'm getting:

[15, 44, 99, 32, 78]
[15, 44, 32, 78, 99]

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.get(ArrayList.java:458)
    at MyClass.main(MyClass.java:10)
spider
  • 15
  • 3
  • 1
    How about `list.sort();` only? – Sudhir Ojha May 27 '21 at 10:42
  • What output are you getting? What output were you expecting? – khelwood May 27 '21 at 10:43
  • Note: `i <= list.size() -1` could be rewritten to `i < list.size()`. – MC Emperor May 27 '21 at 10:44
  • this is what im getting: """[15, 44, 99, 32, 78] [15, 44, 32, 78, 99] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at MyClass.main(MyClass.java:10)""" – spider May 27 '21 at 10:44
  • @spider Please add additional info to the *question* instead of the comment section. Use the [edit] link below the post to do so. – MC Emperor May 27 '21 at 10:45
  • [Edit] your question. It needs to actually say that you're getting an exception and give all the information from the exception. – khelwood May 27 '21 at 10:45
  • It looks like you are implementing bubble sort but you are doing a single pass. – Tarik May 27 '21 at 10:46
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Tom May 27 '21 at 10:48
  • 1
    Your `i` goes up to the last element of the list, at which point `list.get(i+1)` is *after* the end of the list, so it raises an exception. – khelwood May 27 '21 at 10:50

4 Answers4

0

Below code like is the reason for IndexOutOfBoundsException. when i = 4, i + 1 = 5 and no index matching in the List.

    if(list.get(i) > list.get(i+1)){

If you want to try the bubble sort you could try below

 for (int i = list.size() - 1; i >= 0; i--)
        for (int j = 0; j < i; j++) {
            if (list.get(j) > list.get(j + 1)) {
                int temp = list.get(j);
                list.set(j, list.get(j + 1));
                list.set(j + 1, temp);
            }
        }
 System.out.println(list);

If your intention is to sort without your own code, you could use

    Collections.sort(list);
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

This works:

public static void main(String args[]) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(15); 
        list.add(78); 
        list.add(44); 
        list.add(99); 
        list.add(32);
    
        for(int i = 0; i < list.size() - 1; i++){
            for(int j = 0; j < list.size() - i - 1; j++){
                if(list.get(j) > list.get(j + 1)){
                    int temp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, temp);
                }
            }
        }
        System.out.println(list);
    }
Ayush Gupta
  • 1,166
  • 2
  • 9
  • 25
0

First off, it's usually a bad idea to add and/or remove items to a list while looping over it. You are lucky it's working here, because you are adding and removing the same number of items, but it would be better to swap the items at the i and last positions:

int last = list.get(list.size() - 1);
list.set(list.size() - 1) = list.get(i);
list.set(i) = last;

But to your problem: The index of your loop (i) goes from 0 to 4. In the last loop, when you try to list.get(i+1) you attempt to get index 4+1 = 5, but the list only has size of 5, so there is no index 5.

In your case you need to loop to only until the second last index, not the last:

for(int i = 0; i<=list.size() - 2;){
//       Change from 1 to 2 ----^
RoToRa
  • 37,635
  • 12
  • 69
  • 105
0

If you wanna sort manually then this code works :

while  ( i < list.size()-1) {
        if(list.get(i) > list.get(i+1)){
            list.add(list.get(i));
            list.remove(i);
            i--;
        } else {
            i++;
        }
        
    }

But for efficiency I would suggest you to use :

Collections.sort(list);
NBS
  • 71
  • 2