Results 1 to 7 of 7
- 04-13-2009, 03:40 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
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);
}
}
-
set unique to true?
- 04-13-2009, 04:00 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
can you say it more specific? where and how should i change my code?thanks.
-
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?
- 04-13-2009, 04:07 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
oh my god..thanks a lot...i should recognize this earlier..
-
lol, no problem. sometimes you must step away from a problem to see it anew.
-
but the real meat of the code is in the add method where you encounter this:
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.Java Code:if((unique && indexOf(value)== -1) || !unique ){
Similar Threads
-
Avoid/Eliminate Duplicate Code
By kicker in forum New To JavaReplies: 6Last Post: 09-11-2008, 04:14 AM -
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
How to create a Sorted List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:31 PM -
My doublyLinked list does not get sorted
By hasani6leap in forum New To JavaReplies: 0Last Post: 01-06-2008, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks