0

When trying to add, if isFull() is true then need to resize to twice the size.

public boolean add(T element) {
    if (this.contains(element)) {
        return false;
    }

    if (isFull()) {  
        resize(elements.length * 2);
    }
    //etc,etc.
}

//check if full   
private boolean isFull() {
    if (size == elements.length) {
        return true;
    }

    return false;
}

//resize 
private void resize(int capacity)
    T[] a = Arrays.<T>copyOf(elements, capacity);
    elements = a;
}

However, there is an error with my resize method.

Error:ArraySet.java:114: error: cannot find symbol
      T[] a = Arrays.<T>copyOf(elements, capacity);
              ^
  symbol:   variable Arrays
  location: class ArraySet<T>
  where T is a type-variable:
    T extends Comparable<? super T> declared in class ArraySet
1 error

What am I doing wrong? What can I do to resize? How do I resize?

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArraySet<T extends Comparable<? super T>> implements Set<T> {
T[] elements;
int size;

@SuppressWarnings("unchecked")
public ArraySet() {
   elements = (T[]) new Comparable[1];
   size = 0;
}

public int size() {
     return size;
  }
public boolean isEmpty() {
  return size == 0;

}

........ is this enough of the code?

  • Can you please provide the header of the class (i.e., public class ArraySet...) and in addition the type of the elements field – bennyl Oct 14 '16 at 20:14
  • Can you please post the entire class? It's difficult to determine what is going on because it's not clear what some of the variables are referring to. – Gojira Oct 14 '16 at 20:15
  • Is that enough of the code? – Rachel Dao Oct 14 '16 at 20:25
  • Something is still missing. Here are a few observations though. 1) the error you show is a compiler error, meaning that there is either a problem with the source itself or the way you are compiling it. Here is a much more thorough explanation: http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean. 2) Even if you get it to compile, I don't think your code will work. I can't see anywhere you increment the variable 'size' which means that isFull will always return false. – Gojira Oct 14 '16 at 20:51
  • have you imported `java.util.Arrays`? – sidgate Oct 15 '16 at 00:16

0 Answers0