Java Vector remove()

Java Vector [Java Vector] Java Vector


Vector Java remove() Vector

Vector remove()


Vector remove()

// 1.
public E remove(int index)

// 2.
public boolean remove(Object o)

// 3.
public void clear()  // AbstractCollection

1. remove(int index)

1

  • index - 0

  • index < 0 || index >= size() ArrayIndexOutOfBoundsException

import java.util.Vector;

public class VectorRemoveExample {
    public static void main(String[] args) {
        Vector<String> fruits = new Vector<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
       
        System.out.println(": " + fruits);
       
        // 1Banana
        String removed = fruits.remove(1);
       
        System.out.println(": " + removed);
        System.out.println(": " + fruits);
    }
}

: [Apple, Banana, Cherry]
: Banana
: [Apple, Cherry]

2. remove(Object o)

  • o -

  • true
  • false

import java.util.Vector;

public class VectorRemoveObjectExample {
    public static void main(String[] args) {
        Vector<Integer> numbers = new Vector<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(20);  //
       
        System.out.println(": " + numbers);
       
        // 20
        boolean result = numbers.remove(Integer.valueOf(20));
       
        System.out.println(": " + result);
        System.out.println(": " + numbers);
    }
}

: [10, 20, 30, 20]
: true
: [10, 30, 20]

3. clear()

import java.util.Vector;

public class VectorClearExample {
    public static void main(String[] args) {
        Vector<Double> prices = new Vector<>();
        prices.add(19.99);
        prices.add(29.99);
        prices.add(39.99);
       
        System.out.println(": " + prices);
       
        //
        prices.clear();
       
        System.out.println(": " + prices);
        System.out.println(": " + prices.size());
    }
}

: [19.99, 29.99, 39.99]
: []
: 0

remove(int index) int E () ArrayIndexOutOfBoundsException
remove(Object o) Object boolean
clear() void

  1. Vector

  2. remove(int index) O(n)

  3. Vector null remove(null) null

  4. remove(Object o) equals() equals()

  5. Java 1.2 ArrayList Vector


  1. removeAll(Collection c)

  2. Vector Iterator remove() ConcurrentModificationException

  3. trimToSize()

  4. contains(Object o)


Q1: remove(int index) remove(Object o)

remove(int index) remove(Object o) Vector Java

Vector<Integer> vec = new Vector<>();
vec.add(1);
vec.add(2);

vec.remove(1);    // 12
vec.remove(Integer.valueOf(1));  // 1

Q2:

Iterator

Vector<String> vec = new Vector<>();
// ...

Iterator<String> it = vec.iterator();
while (it.hasNext()) {
    String s = it.next();
    if (s.equals("")) {
        it.remove();  //
    }
}

Q3: remove() Vector

remove() Vector sizecapacity trimToSize()


Vector remove() Java Java ArrayList Vector

Java Vector [Java Vector] Java Vector