0

I've made my own Vector class called QueueExtendingVect that is composed of CInteger elements, where CInteger is my own class.

In my code, I'm trying to override Object's clone() method to clone QueueExtendingVect, but keep getting errors. Below is the code:

public Object clone() {
  super.clone();
  QueueExtendingVect copy = (QueueExtendingVect) this.clone(); 

  for(int i = 0; i < this.size(); i++)
    copy.set(i, (CInteger) this.elementAt(i));
}

CInteger class:

package csu.mcdonald;

public class CInteger implements Cloneable {
  private int i; 

  CInteger(int ii) { i = ii; }

  void setI(int ii) { i = ii; }

  int getI() { return this.i; } 

  public Object clone() { 
          return null; 
  }

  public String toString() { 
      Integer bigI = new Integer(this.i); 
      return bigI.toString(); 
  }

  public boolean equals(Object o) {
      if(this.i == ((CInteger)o).i)
          return true; 
      else
          return false;
  }
}

How can I successfully clone Vectors? Thanks :)

Delfino
  • 967
  • 4
  • 21
  • 46
  • What errors are you getting? Also public Object clone() { is not returning anything, is that the real code? Also super.clone() should be assigned to 'copy' not this.clone() – Jim W Mar 11 '14 at 21:17
  • Sorry, I'm supposed to return copy. I'm getting the error "clone() is not visible". I'm not entirely sure what that means. – Delfino Mar 11 '14 at 21:24

1 Answers1

0

This should be sufficient

public Object clone() {
 //EDIT     QueueExtendingVect copy = new QueueExtendingVect(this.size());
 QueueExtendingVect copy = new QueueExtendingVect(this.size());

 for(int i = 0; i < this.size(); i++)
  copy.add(elementAt(i));
 return copy;
}

It's not thread safe by the way.

EDIT: fixed out of bounds issue

Jim W
  • 4,866
  • 1
  • 27
  • 43
  • I'm not seeing how copy is getting the elements from the instance that's calling clone(). It looks to me as thought each cell in copy is being set to whatever is already in that cell. Also, in this assignment, there is no constructor for QueueExtendingVect that initializes a Vector to a specific size. – Delfino Mar 12 '14 at 00:24
  • copy.set(i, elementAt(i)); means that copy's elements are set to the value at 'i' in 'this'. I know you had this.elementAt but the 'this' is superfluous in this context. Use it if you like. – Jim W Mar 12 '14 at 03:15
  • How can you tell that copy.set(i, elementAt(i)); will refer to the element in this? – Delfino Mar 12 '14 at 16:32
  • Because method calls with no object specified are always on 'this'. If it were copy.elementAt then it would be working on 'copy', but since it's not, it's working on 'this'. In my experience 'this' is mostly necessary to differentiate between method variables and class variables of the same name http://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class – Jim W Mar 12 '14 at 16:57
  • I've redone the method, but now I keep getting an ArrayOutOfBoundsException. I'm not sure why this is happening because I'm never exceeding the Vector size of this. How can I repost code here? – Delfino Mar 12 '14 at 17:28
  • Sorry, you need to add to copy, not set. Vectors don't have an initial size, just an initial capacity. So you don't set like an array, you add to them. – Jim W Mar 12 '14 at 17:30
  • So then what's the difference between size and capacity? – Delfino Mar 12 '14 at 17:31
  • Size is the elements it's holding, capacity it the number of elements it can hold before it needs to internally resize itself. – Jim W Mar 12 '14 at 17:33
  • I see what you mean. Thanks! – Delfino Mar 12 '14 at 17:37