2
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.

1
  • What do you exactly mean by I cant generate the last part? Commented Jul 28, 2015 at 12:23

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

But its a random number generator, I'm not setting a value to it. Its not possible here: ideone.com/fEznSZ Am I missing out on something? Or is my input wrong?
@lyj arr[b] = arr[b+1]; is a valid syntax for arrays, not for ArrayLists. Use arr.set(b,arr.get(b+1)); instead.
Still can't, forgive my incompetence. Ideone isn't successfully generating the output. Sorry, am I doing it wrong? ideone.com/uZrTdU
@lyj arr.set(b,arr.get(b)) = arr.set(b,arr.get(b+1)); is wrong. arr.set comes instead of =, not in addition to it.
You mean to take out that line's = ? But when I took out the = it returns a ; as a required input. Sorry, I'm really new to Java (just started 3 days ago), can I get a full picture?
1

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]

9 Comments

Yeah, I'm trying to sort a list of integers without duplicates. Though, I'm not using sorting algorithms like Bubble Sort or Make Unique sort what I'm using is just using the last part of the code segment in my post to sort it out as I'm not trying to learn sorting algorithms just yet, just wanna sort it my way but the array variables I'm using shows forms of error. What the top commenter showed me might be the way but I don't know how to use it to make it work.
Before even trying to code any algorithm, you have to learn the basic fundamentals of how array indexing works. I think you are trying to run before even knowing how to walk. Step back and learn how indexing works before attempting to remove duplicates from an ArrayList. Removing duplicates using your approach is wrong. Either sort it first (as I have done) and traverse the elements properly or use existing Collections such as a SortedSet that does it all. Part of your education should be the familiarization of existing java libraries.
That's the thing, my current study does not allow using existing java libraries but to know how to mathematically sort arrays. I'm not allow to use a library sort function.
The example I gave you to sort doesn't use any library function; it is just my own implementation of a bubble sort. You mean to tell me your teacher expects you to sort an array without using any known sort algorithm?
Pretty much yes, so I'm making use of the one that I've thought up :D
|

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.