Study notes on vector class usage in Java programming
Vector is usually used to implement dynamic arrays, that is, to implement automatically growing object arrays. Like C++, the vector class is also built into Java. Let’s take a look at the basic usage of the vector class.
java.util.vector provides a vector class (vector) to implement functions similar to dynamic arrays. There is no concept of pointers in the Java language, but if pointers are used correctly and flexibly, the quality of the program can indeed be greatly improved. For example, so-called "dynamic arrays" in C and C++ are generally implemented by pointers. In order to make up for this shortcoming, Java provides a rich class library to facilitate programmers to use, and the vector class is one of them. In fact, flexible use of arrays can also complete the functions of the vector class, but the vector class provides a large number of methods, which greatly facilitates user use.
After creating a vector class object, you can insert objects of different classes into it at will, without taking into account the type or pre-selecting the capacity of the vector, and you can easily search. For situations where the array size is unknown or unwilling to be defined in advance, and frequent searches, insertions, and deletions are required. Consider using vector classes.
The Vector class implements a dynamic array. Similar to ArrayList, but different:
Vector is accessed synchronously.
Vector contains many traditional methods that are not part of the collection framework.
Vector is mainly used when the size of the array is not known in advance, or when an array that can be changed is needed.
The Vector class supports 4 construction methods.
1. The first construction method creates a default vector with a default size of 10:
Vector()
2. The second construction method creates a vector of the specified size.
Vector(int size)
3. The third construction method creates a vector of the specified size, and the increment is specified by incr. The increment represents the number of elements added to the vector each time.
Vector(int size,int incr)
4. The fourth construction method creates a vector containing elements of collection c:
Vector(Collection c)
If you use the first method, the system will automatically manage the vector. If you use the latter two methods. Then the system will set the capacity of the vector object (that is, the size of the data that the vector object can store) based on the parameter initialcapacity. When the actual number of stored data exceeds the capacity. The system will expand the vector object storage capacity.
The parameter capacityincrement gives the expansion value of each expansion. When capacityincrement is 0, it will be doubled every time. This function can be used to optimize storage. Various methods are provided in the Vector class to facilitate user use:
Insertion function:
(1) public final synchronized void adddElement(Object obj)
Insert obj into the tail of the vector. obj can be any type of object. For the same vector object, objects of different types can also be inserted into it. However, objects should be inserted instead of values, so when inserting values, be careful to convert the array into the corresponding object.
For example: when you want to insert the integer 1, do not call v1.addElement(1) directly. The correct method is:
Vector v1 = new Vector(); Integer integer1 = new Integer(1); v1.addElement(integer1);
(2)public final synchronized void setElementAt(Object obj,int index)
Set the object at index to obj, and the original object will be overwritten.
(3)public final synchronized void insertElement(Object obj,int index)
Insert obj at the position specified by index, and the original object and subsequent objects are postponed in sequence.
Delete function:
(1)public final synchronized void removeElement(Object obj)
Delete obj from the vector. If there are multiple ones, try starting from the vector head and delete the found first one. A vector member identical to obj.
(2)public final synchronized void removeAllElement();
Delete all objects in the vector
(3)public fianl synchronized void removeElementAt(int index)
Delete the object pointed to by index
Query search function:
(1)public final int indexOf(Object obj)
Start searching for obj from the vector head and return the subscript corresponding to the first obj encountered. If this does not exist obj, returns -1.
(2)public final synchronized int indexOf(Object obj,int index)
Start searching for obj from the subscript represented by index.
(3)public final int lastindexOf( Object obj)
Search obj in reverse from the end of the vector.
(4)public final synchornized int lastIndex(Object obj,int index)
Search obj in reverse from the end to the beginning at the subscript represented by index .
(5)public final synchornized firstElement()
Get the first obj in the vector object
(6)public final synchornized Object lastElement()
Get the last obj of the vector object
Example
The following program illustrates several methods supported by this collection:
import java.util.*; public class VectorDemo { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new Double(5.45)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Float(9.4)); v.addElement(new Integer(10)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Integer(11)); v.addElement(new Integer(12)); System.out.println("First element: " + (Integer)v.firstElement()); System.out.println("Last element: " + (Integer)v.lastElement()); if(v.contains(new Integer(3))) System.out.println("Vector contains 3."); // enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); System.out.println(); } }
The compilation and running results of the above example are as follows:
Initial size: 0 Initial capacity: 3 Capacity after four additions: 5 Current capacity: 5 Current capacity: 7 Current capacity: 9 First element: 1 Last element: 12 Vector contains 3. Elements in vector: 1 2 3 4 5.45 6.08 7 9.4 10 11 12
The above is the usage of vector class in Java programming For the content of study notes, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
