2

My method for removing the duplicate numbers works but not if a number appears more than twice. e.g. a list with numbers 1,2,2,3,4,5,6,7,7,7,8,9 when using the method gives the list 1,2,3,4,5,6,7,7,8,9

import java.util.*;
public class final SortAndRemove{
  private SortAndRemove(){
    }
public static void selectionSort(List<Integer> a){
    if(a == null)
        return;
    if (a.size() == 0 || a.size() == 1)
        return;

    int smallest;
    int smallestIndex;
    for (int curIndex = 0; curIndex < a.size(); curIndex++) {

        smallest = a.get(curIndex);
        smallestIndex = curIndex;

        for (int i = curIndex + 1; i < a.size(); i++) {
            if (smallest > a.get(i)) {

                smallest = a.get(i);
                smallestIndex = i;
            }
        }


        if (smallestIndex == curIndex);

        else {
            int temp = a.get(curIndex);
            a.set(curIndex, a.get(smallestIndex));
            a.set(smallestIndex, temp);
        }

    }
}


public static void removeDuplicates(List<Integer> a){
    if(a == null)
        return;
    if (a.size() == 0 || a.size() == 1)
        return;
    for(int curIndex = 0; curIndex <a.size(); curIndex++){
        int num = a.get(curIndex);
            for(int i = curIndex + 1; i < a.size(); i++){
                if(num == a.get(i))
                    a.remove(i);
            }

    }

}

  }
csanders8
  • 23
  • 6
  • 3
    as far as I am aware a Utility class is just a bunch of useful commonly used/needed functions that can be used in any application. Example include sorting, de-duplication, timing, container classes and etc – Matthew Pigram Jan 21 '14 at 05:29
  • 1
    yeah, looks good. A utility class is basically just a class with a bunch of static methods that you use frequently and for many different things that may or may not be related to each other. Like the static members of System.String could be thought of as a utility class. You might want to declare the class static sealed, though. – Nathan M Jan 21 '14 at 05:29
  • 1
    This is really opinion based as there's no formal definition of "utility class" in Java that I know of. I personally have written classes like your `SortAndRemove` and refer to it as such. Only thing you might add is a private constructor so it can't be instantiated. – Brian Roach Jan 21 '14 at 05:30
  • ok. thanks. so to use this class outside of this project folder how would I able to import it? @BrianRoach – csanders8 Jan 21 '14 at 05:34
  • 1
    @csanders8 same as any other class. – Brian Roach Jan 21 '14 at 05:40

2 Answers2

3

Wikipedia states that a utility class:

is a class that defines a set of methods that perform common, often re-used functions. Most utility classes define these common methods under static (see Static variable) scope.

It's good to give your utility class a private constructor (so that it can never be initialised) i.e.

public class SortAndRemove{
 private SortAndRemove() {
  throw new AssertionError();
 }
 ... // Remainder omitted
}

(This is discussed in Effective Java by Joshua Bloch, by the way)

Catchwa
  • 5,845
  • 4
  • 31
  • 57
0

It's also good to make your utility class final (so no classes can extend from your utility because all methods are static)

public final class SortAndRemove{
 private SortAndRemove() {
  throw new AssertionError();
 }
 ... // Remainder omitted
}
Frank M.
  • 997
  • 1
  • 10
  • 19