Results 1 to 7 of 7
  1. #1
    yinghang is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default i need help with my code.how can i eliminate duplicate number from my sorted list

    import java.util.*;
    // Class SortedDoubleUniqueList can be used to store a list of doubles.

    public class SortedDoubleUniqueList {
    private double[] elementData; // list of doubles
    private int size; // current number of elements in the list
    private boolean unique = false; //duplicates allowed when unique is false

    public static final int DEFAULT_CAPACITY = 100;

    // post: constructs an empty list of default capacity
    public SortedDoubleUniqueList() {
    this(DEFAULT_CAPACITY);
    }

    // pre : capacity >= 0
    // post: constructs an empty list with the given capacity
    public SortedDoubleUniqueList(int capacity) {
    elementData = new double[capacity];
    size = 0;
    }

    // post: returns the current number of elements in the list
    public int size() {
    return size;
    }

    // pre : 0 <= index < size()
    // post: returns the double at the given index in the list
    public double get(int index) {
    return elementData[index];
    }

    // post: creates a comma-separated, bracketed version of the list
    public String toString() {
    if (size == 0) {
    return "[]";
    } else {
    String result = "[" + elementData[0];
    for (int i = 1; i < size; i++)
    result += ", " + elementData[i];
    result += "]";
    return result;
    }
    }

    //post : returns the position of the first occurence of the given
    //value (-1 if not found)
    public double indexOf(double value) {
    int position = Arrays.binarySearch(elementData,0,size,value);
    if(position < 0){
    return -1;
    }
    return position;
    }

    // pre : size() < capacity (elementData.length)
    // post: appends the given value to the end of the list
    public void add(double value) {
    if((unique && indexOf(value)== -1) || !unique ){
    int positionIndex=0;
    for (int i=0;i<size;i++){
    if (value>=elementData[i]){
    positionIndex++;
    }else{
    break;
    }
    }
    for (int i=size;i>positionIndex;i--){
    elementData[i]=elementData[i-1];
    }
    elementData[positionIndex]=value;
    size++;
    }
    }
    }



    Here is test code:


    public class SortedDoubleUniqueListTest {
    public static void main(String[] args) {
    SortedDoubleUniqueList list = new SortedDoubleUniqueList();

    list.add(1.0);
    list.add(5.1);
    list.add(-3.6);
    list.add(0);
    list.add(10.03);
    list.add(100);
    list.add(5.1);
    list.add(5.1);


    System.out.println("list = " + list);

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    set unique to true?

  3. #3
    yinghang is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    can you say it more specific? where and how should i change my code?thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    reread this line of code and the comment:
    private boolean unique = false; //duplicates allowed when unique is false

    that tells me (and the program logic makes sense) that if unique is set to true here, duplicates will not be allowed. Why not try it and see what happens?

  5. #5
    yinghang is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    oh my god..thanks a lot...i should recognize this earlier..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    lol, no problem. sometimes you must step away from a problem to see it anew.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    but the real meat of the code is in the add method where you encounter this:
    Java Code:
    if((unique && indexOf(value)== -1) || !unique ){
    This states if unique is true and the value cannot be found (there is no match), then add the value. Also add the value if unique is false, regardless of whether a match is found or not.

Similar Threads

  1. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 04:14 AM
  2. Replies: 7
    Last Post: 04-28-2008, 04:28 AM
  3. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  4. How to create a Sorted List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:31 PM
  5. My doublyLinked list does not get sorted
    By hasani6leap in forum New To Java
    Replies: 0
    Last Post: 01-06-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •