Java by API/java.util/Vector
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 new Vector(Collection c)
- 2 Save Vector to File
- 3 Vector: addElement(E e)
- 4 Vector: add(T e)
- 5 Vector: capacity()
- 6 Vector: clear()
- 7 Vector: containsAll(Collection<?> c)
- 8 Vector: contains(Object obj)
- 9 Vector: copyInto(Object[] anArray)
- 10 Vector: elementAt(int index)
- 11 Vector: elements()
- 12 Vector: ensureCapacity(int minCapacity)
- 13 Vector: equals(Object o)
- 14 Vector: firstElement()
- 15 Vector: get(int index)
- 16 Vector: hashCode()
- 17 Vector: indexOf(Object o)
- 18 Vector: indexOf(Object o, int index)
- 19 Vector: insertElementAt(E obj, int index)
- 20 Vector: isEmpty()
- 21 Vector: iterator()
- 22 Vector: lastElement()
- 23 Vector: lastIndexOf(Object o)
- 24 Vector: lastIndexOf(Object o, int index)
- 25 Vector: listIterator()
- 26 Vector: removeAll(Collection<?> c)
- 27 Vector: removeAllElements()
- 28 Vector: removeElementAt(int index)
- 29 Vector: removeElement(Object obj)
- 30 Vector: remove(int index)
- 31 Vector: remove(Object o)
- 32 Vector: retainAll(Collection<?> c)
- 33 Vector: set(int index, Object element)
- 34 Vector: setSize(int newSize)
- 35 Vector: size()
- 36 Vector: subList(int fromIndex, int toIndex)
- 37 Vector: toArray()
- 38 Vector: toArray(T[] a)
new Vector(Collection c)
import java.util.Arrays;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Integer[] array = {1,2,3,4,5};
Vector<Integer> v = new Vector<Integer>(Arrays.asList(array));
System.out.println(v);
}
}
Save Vector to File
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
public class MainClass {
public static void main(String args[]) throws Exception {
String[] a = new String[]{"a","b","c"};
Vector v = new Vector(Arrays.asList());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(v);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Vector v2 = (Vector) ois.readObject();
Enumeration e = v.elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}
Vector: addElement(E e)
/*
* Output:
5.0 5.0 5.0 5.0 5.0
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
Vector v = new Vector();
for(int i = 0; i < 5; i++) {
v.addElement(new Double(5));
}
for(int i = v.size() - 1; i >= 0; i--) {
System.out.print(v.elementAt(i) + " ");
}
}
}
Vector: add(T e)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v = new Vector<String>();
v.add("Hello");
v.add("Hello");
String s = v.get(0);
System.out.println(s);
}
}
Vector: capacity()
/**
*Output:
Initial size: 0
Initial capacity: 3
First element: 1
Last element: 2
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());
if (v.contains(3))
System.out.println("Vector contains 3.");
}
}
Vector: clear()
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(i,0);
}
System.out.println(v);
v.clear();
System.out.println(v);
}
}
Vector: containsAll(Collection<?> c)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
Vector<String> v2 = new Vector<String>();
v2.add("A");
v2.add("B");
v2.add("C");
boolean isContaining = v2.containsAll(v);
System.out.println(isContaining);
}
}
Vector: contains(Object obj)
/**
*Output:
Initial size: 0
Initial capacity: 3
First element: 1
Last element: 2
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());
if (v.contains(3))
System.out.println("Vector contains 3.");
}
}
Vector: copyInto(Object[] anArray)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
String array[] = new String[v1.size()];
v1.copyInto(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
Vector: elementAt(int index)
/*
* Output:
5.0 5.0 5.0 5.0 5.0
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
Vector v = new Vector();
for(int i = 0; i < 5; i++) {
v.addElement(new Double(5));
}
for(int i = v.size() - 1; i >= 0; i--) {
System.out.print(v.elementAt(i) + " ");
}
}
}
Vector: elements()
/*
* Output:
5
-14.14
Hello
*/
import java.util.Enumeration;
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
Vector vector = new Vector();
vector.addElement(new Integer(5));
vector.addElement(new Float(-14.14f));
vector.addElement(new String("Hello"));
Enumeration e = vector.elements();
while(e.hasMoreElements()) {
Object obj = e.nextElement();
System.out.println(obj);
}
}
}
Vector: ensureCapacity(int minCapacity)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
System.out.println(v.size());
v.ensureCapacity(40);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
System.out.println(v.size());
}
}
Vector: equals(Object o)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Integer> v = new Vector<Integer>(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
Vector<Integer> v2 = new Vector<Integer>(5);
for (int i = 0; i < 10; i++) {
v2.add(0,i);
}
System.out.println(v2);
System.out.println(v2.equals(v));
v2.removeAll(v);
System.out.println(v2);
}
}
Vector: firstElement()
/**
*Output:
Initial size: 0
Initial capacity: 3
First element: 1
Last element: 2
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());
if (v.contains(3))
System.out.println("Vector contains 3.");
}
}
Vector: get(int index)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v = new Vector<String>();
v.add("Hello");
v.add("Hello");
String s = v.get(0);
System.out.println(s);
}
}
Vector: hashCode()
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
System.out.println(v1.hashCode());
}
}
Vector: indexOf(Object o)
/*
Output:
[A, B, C]
Contains A?: true
Where"s A?: 0
Where"s B from end?: 1
C
* */
import java.util.Vector;
public class MainClass {
static String members[] = { "A", "B", "C"};
public static void main(String args[]) {
Vector v = new Vector();
for (int i = 0, n = members.length; i < n; i++) {
v.add(members[i]);
}
System.out.println(v);
System.out.println("Contains A?: " + v.contains("A"));
System.out.println("Where"s A?: " + v.indexOf("A"));
System.out.println("Where"s B from end?: " + v.lastIndexOf("B"));
int index = 0;
int length = v.size();
while ((index < length) && (index >= 0)) {
index = v.indexOf("C", index);
if (index != -1) {
System.out.println(v.get(index));
index++;
}
}
}
}
Vector: indexOf(Object o, int index)
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
v.add("1");
v.add("2");
System.out.println(v.indexOf("1", 4));
System.out.println(v.lastIndexOf("2", 5));
}
}
Vector: insertElementAt(E obj, int index)
/*
* Output:
[5, -14.14]
[5, String to be inserted, -14.14]
[5, String to be inserted]
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
Vector vector = new Vector();
vector.addElement(new Integer(5));
vector.addElement(new Float(-14.14f));
System.out.println(vector);
String s = new String("String to be inserted");
vector.insertElementAt(s, 1);
System.out.println(vector);
vector.removeElementAt(2);
System.out.println(vector);
}
}
Vector: isEmpty()
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
v.setSize(0);
System.out.println(v);
System.out.println(v.isEmpty());
}
}
Vector: iterator()
import java.util.Iterator;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
Iterator i = v.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
Vector: lastElement()
/**
*Output:
Initial size: 0
Initial capacity: 3
First element: 1
Last element: 2
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());
if (v.contains(3))
System.out.println("Vector contains 3.");
}
}
Vector: lastIndexOf(Object o)
import java.util.Vector;
public class Main {
static String members[] = { "A", "I", "C", "D", "E", "F", "G", "H", "I", "J" };
public static void main(String args[]) {
Vector v = new Vector();
for (int i = 0, n = members.length; i < n; i++) {
v.add(members[i]);
}
System.out.println(v);
System.out.println(v.lastIndexOf("I"));
}
}
Vector: lastIndexOf(Object o, int index)
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
v.add("1");
v.add("2");
System.out.println(v.indexOf("1", 4));
System.out.println(v.lastIndexOf("2", 5));
}
}
Vector: listIterator()
import java.util.ListIterator;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
ListIterator itr = v.listIterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
while (itr.hasPrevious()){
System.out.println(itr.previous());
}
}
}
Vector: removeAll(Collection<?> c)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Integer> v = new Vector<Integer>(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
Vector<Integer> v2 = new Vector<Integer>(5);
for (int i = 0; i < 10; i++) {
v2.add(0,i);
}
System.out.println(v2);
System.out.println(v2.equals(v));
v2.removeAll(v);
System.out.println(v2);
}
}
Vector: removeAllElements()
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("1");
v.add("2");
v.add("3");
System.out.println(v.size());
v.clear();
v.removeAllElements();
System.out.println(v.size());
}
}
Vector: removeElementAt(int index)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v.capacity());
System.out.println(v);
v.remove(1);
v.removeElementAt(2);
System.out.println(v);
System.out.println(v.capacity());
}
}
Vector: removeElement(Object obj)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v.capacity());
System.out.println(v);
v.remove(new Integer(9));
v.removeElement(new Integer(2));
System.out.println(v);
System.out.println(v.capacity());
}
}
Vector: remove(int index)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v.capacity());
System.out.println(v);
v.remove(1);
v.removeElementAt(2);
System.out.println(v);
System.out.println(v.capacity());
}
}
Vector: remove(Object o)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v.capacity());
System.out.println(v);
v.remove(new Integer(9));
v.removeElement(new Integer(2));
System.out.println(v);
System.out.println(v.capacity());
}
}
Vector: retainAll(Collection<?> c)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Integer> v = new Vector<Integer>(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
Vector<Integer> v2 = new Vector<Integer>(5);
for (int i = 0; i < 5; i++) {
v2.add(0,i);
}
System.out.println(v2);
System.out.println(v2.equals(v));
v.retainAll(v2);
System.out.println(v);
}
}
Vector: set(int index, Object element)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Object> v = new Vector<Object>(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
for (int i = 0; i < 10; i++) {
v.set(i,"A");
}
System.out.println(v);
}
}
Vector: setSize(int newSize)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Object> v = new Vector<Object>(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
v.setSize(3);
System.out.println(v);
}
}
Vector: size()
/**
*Output:
Initial size: 0
Initial capacity: 3
First element: 1
Last element: 2
*/
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());
if (v.contains(3))
System.out.println("Vector contains 3.");
}
}
Vector: subList(int fromIndex, int toIndex)
import java.util.List;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
List l = v1.subList(1, 2);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
}
}
Vector: toArray()
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
Object[] array = v1.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
Vector: toArray(T[] a)
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
String array[] = new String[0];
array = (String[]) v1.toArray(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}