This article mainly introduces the detailed explanation of the use of Java vector class and related information of examples. Friends in need can refer to the following
Basic operation examples
VectorApp.java
import java.util.Vector; import java.lang.*; import java.util.Enumeration; public class VectorApp { public static void main(String args[]) { Vector v1 = new Vector(); Integer integer1= new Integer(1); //加入为字符串对象 v1.addElement("one"); //加入的为integer的对象 v1.addElement(integer1); v1.addElement(integer1); v1.addElement("two"); v1.addElement(new Integer(2)); v1.addElement(integer1); v1.addElement(integer1); //转为字符串并打印 System.out.println("The Vector v1 is:\n\t"+v1); //向指定位置插入新对象 v1.insertElement("three",2); v1.insertElement(new Float(3.9),3); System.out.println("The Vector v1(used method insertElementAt()is:\n\t)"+v1); //将指定位置的对象设置为新的对象 //指定位置后的对象依次往后顺延 v1.setElementAt("four",2); System.out.println("The vector v1 cused method setElmentAt()is:\n\t"+v1); v1.removeElement(integer1); //从向量对象v1中删除对象integer1 //由于存在多个integer1,所以从头开始。 //找删除找到的第一个integer1. Enumeration enum = v1.elements(); System.out.println("The vector v1 (used method removeElememt()is"); while(enum.hasMoreElements()) System.out.println(enum.nextElement()+""); System.out.println(); //使用枚举类(Enumeration)的方法取得向量对象的每个元素。 System.out.println("The position of Object1(top-to-botton):"+v1.indexOf(integer1)); System.out.println("The position of Object1(tottom-to-top):"+v1.lastIndexOf(integer1)); //按不同的方向查找对象integer1所处的位置 v1.setSize(4); System.out.println("The new Vector(resized the vector)is:"+v1); //重新设置v1的大小,多余的元素被抛弃 } }
Run result:
E:\java01>java VectorApp The vector v1 is:[one,1,1,two,2,1,1] The vector v1(used method insetElementAt()) is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(useed method removeElement()) is: one four 3.9 1 two 2 1 1 The position of object1(top-to-botton):3 The position of object1(botton-to-top):7 The new Vector(resized the vector) is: [one,four,3.9,1]
1x expansion of Vertor
Do you still remember that ArrayList expands to 0.5 times the size of array each time? Vector is slightly different from ArrayList when performing expansion operations
protected int capacityIncrement;//用于指定每次扩容的容量 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);//如不指定capacityIncrement,默认扩容的容量为原数组的容量 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }Careful friends can find that there is an additional capacityIncrement
Vertor is thread-safe!
private static Vector<Integer> vector=new Vector<Integer>(); public static void main(String[] args) { while(true) { for(int i=0;i<10;i++) { vector.add(i); } Thread removeThread=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<vector.size();i++) { vector.remove(i); } } }); Thread printThread=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<vector.size();i++) { System.out.println(vector.get(i)); } } }); removeThread.start(); printThread.start(); while(Thread.activeCount()>20); } }When everyone runs this code After running for a short period of time, you will find an
ArrayIndexOutOfBoundsException exception. Here, the
Vector's get, remove, size methods are
synchronized modified, but in a multi-threaded environment , if no additional synchronization measures are taken on the method side, this code is still unsafe. If one thread deletes the element with serial number i, and another thread accesses this i, it will directly throw an exception back, so this code is guaranteed to Security needs to be modified by adding
synchronized inside
run.
Java Basic Introduction Video Tutorial
3.The above is the detailed content of Detailed explanation of instance methods of vector class. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment