nicotineinfused
nicotineinfused

Reputation: 85

Problems in ArrayList<Integer>

import java.util.*;
import java.util.Random;

class ArraySorting {
public static void main(String[]args) {

    ArrayList<Integer> arr = new ArrayList<Integer>();

    Random generate = new Random();
    for (int nums = 0; nums < 20; nums++) {
      int randomnumbers = generate.nextInt(50);
      arr.add(randomnumbers);
    }

    System.out.println("First list of 20 generated numbers: ");
    System.out.println(arr);
    System.out.println("");

    int dupe = 0; 

    for (int n = 0; n < arr.size(); n++) {
        Integer check1 = arr.get(n); 

        for (int n2 = n+1; n2 < arr.size(); n2++) { 
            Integer check2 = arr.get(n2); 

            //remove second num if two numbers akike
            if (check1.equals(check2)) {
                arr.remove(check2);
                n2 = n2-1;
                dupe = 1; 

            }
        }
        n = n-dupe;
        dupe = 0;
    }

    System.out.println("Duplicates: " + (20 - arr.size()));

    for (int n3 = arr.size(); n3 < 20; ++n3) {
        int randomnumbers = generate.nextInt(50);
        arr.add(randomnumbers);

        //check for duplicates again
        for (int n = 0; n < arr.size(); n++) {
            Integer check1 = arr.get(n); 

            for (int n2 = n+1; n2 < arr.size(); n2++) { 
                Integer check2 = arr.get(n2); 

                if (check1.equals(check2)) {
                    arr.remove(check2);
                    n2 = n2-1;
                    dupe = 1; 
                }
            }
            n = n - dupe;
            dupe = 0;
        }
    }

    //before sort
    System.out.println(arr);
    System.out.println("");

    for(int a=0; a<20; a++){
        for (int b = 0; b < 19; b++) {
            if(arr[b] > arr[b+1]){
                int temporary = arr[b];
                arr[b] = arr[b+1];
                arr[b+1] = temporary;
            }
        }
    }

    System.out.println("\nSorted Array:\n");
    for (int a = 0; a < 20; a++) {
        System.out.println("Array [" + a + "]: " + arr[a]);
    }


}

}

Can anyone tell me what I did wrong for this one, I can't seem to generate the last part. Shouldn't the ArrayList arr = new ArrayList(); run same with the last part where arr[b] is work? I'm new to Java so would greatly appreciate if simple explaination/metaphor is ued provided with solution.

P.S: I'm not planning to use a library sort function like Collection, I'm required to use the sorting method at the last part.

Upvotes: 2

Views: 197

Answers (2)

Constantin
Constantin

Reputation: 1506

The problem you are having is that you are trying to remove your duplicates before sorting. First, sort your integers, duplicates and all, and then remove the duplicates.

import java.util.ArrayList;
import java.util.Random;

public class ArraySorting {
    public static void main(String[]args) {

        ArrayList<Integer> arr = new ArrayList<Integer>();

        Random generate = new Random();
        for (int nums = 0; nums < 20; nums++) {
            int randomnumbers = generate.nextInt(10);
            arr.add(randomnumbers);
        }

        System.out.println("First list of 20 generated numbers: ");
        System.out.println(arr);
        System.out.println("");

        // SORT YOUR LIST FIRST
        bubbleSort(arr);
        System.out.println(arr);

        // NOW YOU CAN REMOVE YOUR DUPLICATES
        removeDuplicates(arr);
        System.out.println(arr);
    }

    public static void bubbleSort(ArrayList<Integer> list){
        for(int i = 0; i < list.size(); i++) {
            for(int j = 1; j < (list.size() -i); j++) {
                if(list.get(j - 1) > list.get(j)) {
                    int temp = list.get(j-1);
                    list.set(j-1, list.get(j));
                    list.set(j, temp);
                }                   
            }
        }       
    }

    public static void removeDuplicates(ArrayList<Integer> list){
        for(int i = 0; i < list.size(); i++) {
            if(i < list.size()-1) {
                int prev = list.get(i);
                int curr = list.get(i + 1);

                if(curr == prev) {
                    list.remove(list.get(i + 1));
                    i--;
                }
            }   
        }
    }   
}

Output

First list of 20 generated numbers: 
[9, 2, 2, 1, 3, 4, 0, 9, 5, 2, 5, 7, 4, 9, 0, 4, 0, 6, 6, 6]

[0, 0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 9, 9, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 9]

Upvotes: 1

Eran
Eran

Reputation: 393801

arr[a] is the syntax to access array elements. For ArrayLists you use arr.get(a). And to assign a value to an ArrayList, you use arr.set(b,value). You can't use the assignment operator.

Upvotes: 6

Related Questions