Java .

Java Vectors

java.util.Vector

the Vector constructors

Vector vectorName = new Vector();

Vector vectorName = new Vector(5); //The 5 sets the initial element capcaity. // The default is 10.

Vector vectorName = new Vector(5,9); //The 5 sets the initial element capacity. // The default is 10. //The 9 sets the number of elements to grow // capacity by on overflow. // The default is two times current capacity.

Vector vectorName = new Vector(collectionIn); //creates a new vector with the elements of a collection. //Elements are read from the collection with an Iterator.

Vector methods to find out things about the Vector

int vectorSize = vectorName.size();

boolean isVectorEmpty = vectorName.isEmpty();

Vector methods to find out what is in the Vector

int objectFirstFoundAt = 
  vectorName.indexOf(objectToSearchFor);
int objectFirstFoundAt = 
  vectorName.indexOf(objectToSearchFor,
                     intOffsetToStartSearchAt);

int objectLastFoundAt = vectorName.lastIndexOf(objectToSearchFor); int objectLastFoundAt = vectorName.lastIndexOf(objectToSearchFor, intOffsetToStartSearchAt);

boolean isObjectInVector = vectorName.contains(objectToSearchFor);

boolean doesContainAllOfCollection = vectorName.containsAll(collectionToSearchForAllOf);

Object objectName = vectorName.elementAt(intToGetElementAt);

Object objectName = vectorName.get(intToGetElementAt);

Object firstObjectName = vectorName.firstElement();

Object lastObjectName = vectorName.lastElement();

List partialList = vectorName.subList(offsetToStartAt, offsetToEndAt);

Vector methods to alter what is in the Vector

vectorName.addElement(objectToAdd);

vectorName.add(objectToAdd); //inserts at end

vectorName.add(intOffsetToAddElementAt, objectToAdd); //inserts at offset

vectorName.insertElementAt(objectIn, intOffsetToSetElementAt); //inserts at offset

vectorName.addAll(collectionToAdd); vectorName.addAll(intOffsetToAddCollectionAt, collectionToAdd);

vectorName.setElementAt(objectIn, intOffsetToSetElementAt); //replaces element at offset

vectorName.set(intOffsetToSetElementAt, objectIn); //replaces element at offset

vectorName.removeElementAt(intOffsetToRemoveElement); //deletes element at offset

vectorName.remove(intOffsetToRemoveElement); //deletes element at offset

vectorName.removeAll(collectionToRemoveAllElementsOf); //deletes all elements in the Collection from the Vector

vectorName.retainAll(collectionToKeepAllElementsOf); //deletes all but elements in the Collection from the Vector

vectorName.removeRange(collectionToKeepAllElementsOf); //deletes from start offset up to the end offset //note - element at the end offset is not deleted

vectorName.removeAllElements();

vectorName.clear();

Vector methods for Arrays

vectorName.copyInto(arrayName);
Comments
Sign in to be the first to comment on Java Vectors.