在Java中使用向量? (特别是构造函数)

时间:2014-01-21 22:24:13

标签: java vector

我很确定我没有正确创建构造函数。我正在使用Vectors(不是选择)的事实让我很困惑。我做对了吗? Vector有深度复制方法吗?

感谢您的帮助。

import java.util.Vector; 
import java.util.Iterator;

public class ListVectorBased<E> implements ListInterface<E>, Iterable<E> {

    /** array on which the list is based */
    private Vector<E> items;    

    /** default Constructor */
    public ListVectorBased() {      
    }

    /** constructor with the first item: constructs a list with the
     * specified item as single element of this list
     * @param item first element of the list
     */
    public ListVectorBased(E elt) {
        items.add(1, elt);
    }

    /** copy constructor: create a duplicate of the specified list
     * @param list to be copied
     */
    public ListVectorBased(Vector<E> items) {
        this.items = items;
    }


    /** Tests if this list has no elements.
     * @return <code>true</codeif this list has no elements;
     * <code>false</codeotherwise.
     */
    public boolean isEmpty() {
        return items.isEmpty();
    } // end isEmpty


    /**
     * Returns the number of elements in this list.
     * @return the number of elements in this list.
     */
    public int size() {
        return items.size();
    }  // end size

    /**
     * Remove all the elements in this list.
     */
    public void removeAll() {
        items.removeAllElements();
    } // end removeAll


    /**
     * Inserts the specified element at the specified position in this
     * list. Shifts the element currently at that position (if any)
     * and any subsequent elements to the right (adds one to their
     * indices).
     * @param index index at which the specified element is to be
     * inserted.
     * @param item element to be inserted.
     * @throws IndexOutOfBoundsException - if index is out of range
     * (index < 0 || index size()).
     */
    public void add(int index, E item) throws ListIndexOutOfBoundsException {
        if (index >= 1 && index <= items.size()+1) {
            items.add(index-1,item);
        } else {  // index out of range
            throw new ListIndexOutOfBoundsException("ListIndexOutOfBoundsException on add");
        }  // end if
    } //end add


    /**
     * appends the specified element to the end of this list.
     * @param elt element to be added at the end of the list
     */
    public void append(E elt) {
        add (items.size()+1, elt);
    }

    /**
     * Returns the element at the specified position in this list.
     * @param index index of element to return.
     * @return the element at the specified position in this list.
     * @throws IndexOutOfBoundsException - if index is out of range
     * (index < 0 || index size()).
     */
    public E get(int index)     throws ListIndexOutOfBoundsException {  
        if (index >= 1 && index <= items.size()) {
            return items.get(index-1);
        } else {  // index out of range
            throw new ListIndexOutOfBoundsException(
                            "ListIndexOutOfBoundsException on get");    
        }  // end if
    } // end get


    /**
     * Removes the element at the specified position in this
     * list. Shifts any subsequent elements to the left (subtracts one
     * from their indices).
     * @param index the index of the element to remove
     * @throws IndexOutOfBoundsException - if index is out of range
     * (index < 0 || index size()).
     */
    public void remove(int index) throws ListIndexOutOfBoundsException {
        if (index >= 1 && index <= items.size()) {
            // delete item by shifting all items at 
            // positions index toward the beginning of the list
            // (no shift if index == size)
            items.remove(index-1);
        } else {  // index out of range
            throw new ListIndexOutOfBoundsException(
                            "ListIndexOutOfBoundsException on remove");
        }  // end if
    } //end remove


    /**  delete
     * delete the the specified element in this list if exists. Shifts
     * any subsequent elements to the left (subtracts one from their
     * indices).
     * @param elt the element, if it exists, to delete
     */
    public void delete (E elt) {
        if (items.contains(elt)) {
            items.remove(elt);
        }
    }

    /** contains
     * Looks for the index of the specified element in this list. If
     * the element is not present, the method returns <code>-1</code>
     * @param elt the element which index is looked for.
     * @return either the index of the location in the list where the
     * argument is present or <code>-1</codeif the argument is not
     * contained in the list.
     */
    public int contains(E elt) {
        for (int idx = 1; idx <= items.size(); idx++) {
            if (items.get(idx).equals(elt))
                return idx;
            else
                return -1;
        } // end for
    } //end contains


    /** display
     * Prints all the elements in this list on the console in sequence
     */
    public void display() {
        for (int idx = 1; idx <= items.size(); idx++) {
            System.out.print(items.get(idx));
        } //end for
    } //end display



    /** method to make the class iterable */

    public Iterator<E> iterator(){
        return items.iterator();
    }

}  // end ListVectorBased

1 个答案:

答案 0 :(得分:0)

我看到的最大问题是某些构造函数初始化items而其他构造函数不初始化,但其他代码假定项目为非null。我的建议是创建项目final,始终创建一个新的向量,然后根据需要将通过构造函数传入的任何项目复制到新的私有向量。

(注意,目前,在你将vector作为参数的构造函数中,你只是复制对向量的引用,这意味着,你不知道,一些外部代码可能会搞乱你的“私有”任何时候的矢量。那种方式就是疯狂......)

即:

/** array on which the list is based */
private final Vector<E> items=new Vector<E>();    

/** default Constructor */
public ListVectorBased() {      
}

/** constructor with the first item: constructs a list with the
 * specified item as single element of this list
 * @param item first element of the list
 */
public ListVectorBased(E elt) {
    items.add(elt);
}

/** copy constructor: create a duplicate of the specified list
 * @param list to be copied
 */
public ListVectorBased(Vector<E> items) {
    this.items.addAll(items);
}