-3

I was messing around with data structures and while creating a Dynamic Array in Java from scratch, I figured out that my delete method (in the DynamicArray class) was not working for doubles. I've read some documentation and I realized that doubles in Java are treated differently when it comes to comparison. So I found ways to compare just doubles, but what do you do when the comparison is part of random data (Object type)?

Here is my code:

---Main---

public class Main {

    public static void main(String[] args) {
        
        //ArrayList<String> dynamicArray = new ArrayList<>();
        
        DynamicArray dynamicArray = new DynamicArray();
            
        System.out.println(dynamicArray.isEmpty());
        
        dynamicArray.add(7);
        dynamicArray.add(15);
        dynamicArray.add(13);
        dynamicArray.add("A");
        dynamicArray.add('C');
        dynamicArray.add(3.14159);
        
        System.out.println("size: "+ dynamicArray.size());
        System.out.println("capacity: "+ dynamicArray.capacity());
        
        System.out.println(dynamicArray.toString());
        
        dynamicArray.insert(2, "Hello");
        System.out.println(dynamicArray.toString());
        
    
        
        dynamicArray.delete(3.14159);
        System.out.println("After removing 3.14159: "+ dynamicArray.toString( ));
        dynamicArray.delete(7);
        System.out.println(dynamicArray.toString());
        
    }
}

---DynamicArray---

public class DynamicArray {

    final double EPSILON = 0.000001d;
    private int size;
    private int capacity = 7;
    Object[] array; // created an array of Objects named array
    
    
    public DynamicArray() {
        this.array = new Object[capacity];
    }
    
    public DynamicArray(int capacity) {
        this.capacity = capacity;
        this.array = new Object[capacity];
    }
    
    public void add(Object data) {
        if (size >= capacity) {
            grow();
        }
        array[size] = data;
        size++;
    }
    
    public void insert(int index, Object data) {
        if (size >= capacity) {
            grow();
        }
        for (int i = size; i > index; i--) {
            array[i] = array[i-1];
        }
        
        array[index] = data;
        size++;
    }
    
    public void delete(Object data) {

        for(int i = 0; i < size; i++) {
            if(array[i] == data) {
                for(int j = 0; j < (size - i - 1); j++){
                    array[i + j] = array[i + j + 1];
                }
                array[size - 1] = null;
                size--;
                if(size <=(int) (capacity/3)) {
                    shrink();
                }
                break;
            }
        }
        
    }
    
    public int search(Object data) {
        return -1;
    }
    
    public void grow() {
        
    }
    
    public void shrink() {
        
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public Object peek() {
        
        return array[size-1];
    }
    
    public String toString() {
        String string = "[";
        for (int i = 0; i < capacity; i++) {
            string += array[i] + ", ";
        }
        
        
        if (string != "[") {
            string = string.substring(0,string.length()-2);
        }
        
        return string+"]";
    }
    
    public int size() {
        return size;
    }
    
    public int capacity() {
        return capacity;
    }
}

Am I missing something?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    _"doubles in Java are treated differently when it comes to comparison"_ - are you sure this is the problem? You need to provide more details, such as an example of a comparison that fails according to your mental model. You have other issues as well: `string != "["` will not do what you want, strings must be compared with `equals()`. – Jim Garrison Apr 02 '22 at 00:21
  • Are you saying you want to compare int `2` to `BigInteger.valueOf(2)` or to `2.0, ,etc, etc`. or do you just want to compare to like types and return false if different types and true if equal and same type? – WJS Apr 02 '22 at 00:24
  • This is a dup, of https://stackoverflow.com/q/16069106/18157 and https://stackoverflow.com/q/13387742/18157 but I used up my close vote already. – Jim Garrison Apr 02 '22 at 00:31

2 Answers2

1

Objects in Java must be compared with equals(), not ==.

public void delete(Object data) {

    for(int i = 0; i < size; i++) {
        if(array[i] == data) {
            for(int j = 0; j < (size - i - 1); j++){

The if(array[i] == data) will compare object references, not the objects' values.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
-1

you can use equals method of object, this way you don't need to use comparisons but you should check for null values.

public void delete(Object data) {

    for(int i = 0; i < size; i++) {
        if(array[i]!=null &&  array[i].equals(data)) {
            for(int j = 0; j < (size - i - 1); j++){
                array[i + j] = array[i + j + 1];
            }
            array[size - 1] = null;
            size--;
            if(size <=(int) (capacity/3)) {
                shrink();
            }
            break;
        }
    }
}
ahtasham nazeer
  • 137
  • 1
  • 7