Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Vector container in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the Vector container in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

I. Preface

Knowledge supplement: Arrays.copyOf function:

Public static int [] copyOf (int [] original, int newLength) {int [] copy = new int [newLength]; System.arraycopy (original, 0, copy, 0, Math.min (original.length, newLength)); return copy;}

You can see that copyOf () creates a new array internally, and calls arrayCopy () to copy the original contents into copy, and the length is newLength. Return to copy

Let's take a look at the System.arraycopy function:

Public static native void arraycopy (Object src, int srcPos, Object dest, int destPos, int length)

Src-Source array.

SrcPos-the starting position in the source array.

Dest-Target array.

DestPos-the starting position in the target data.

Length-the number of array elements to copy.

This method uses the native keyword and calls the underlying function written for C++, which shows that it is the underlying function in JDK.

II. Brief introduction to Vector public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable

The Vector class implements a growable array of objects that stores data internally in the form of dynamic arrays.

Vector has the characteristics of arrays and supports random access through indexes, so it is very efficient to access elements in Vector randomly, but it is inefficient to perform inserts and deletions.

Inherits AbstractList, which provides a backbone implementation of the List interface to minimize the work required to implement the interface supported by "random access" data stores, such as arrays. For continuous access data (such as linked lists), AbstractSequentialList should be preferred over this class.

The implementation of the List interface means that Vector elements are ordered, repeatable, and can have a collection of null elements.

The implementation of the RandomAccess interface indicates that it supports random fast access. In fact, when we look at the RandomAccess source code, we can see that there is nothing defined. Because the underlying ArrayList is an array, then random fast access is taken for granted, access speed O (1).

Implements the Cloneable interface, indicating that it can be copied. Note that the clone () replication in ArrayList is actually a shallow copy

Implements the Serializable identity collection that can be serialized.

3. Vector source code public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable {/ / an array of Vector data protected Object [] elementData; / / number of actual data protected int elementCount; / / capacity growth coefficient protected int capacityIncrement; / / Vector sequence version number private static final long serialVersionUID =-2767605614048989439L / / the constructor public Vector (int initialCapacity, int capacityIncrement) {super () that specifies the initial size and growth factor of Vector; if (initialCapacity < 0) throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity); this.elementData = new Object [initialCapacity]; this.capacityIncrement = capacityIncrement } / / Constructor public Vector (int initialCapacity) {this (initialCapacity, 0);} / / Vector constructor. The default capacity is 10 public Vector () {this (10);} / / initialize a constructor public Vector (Collection) that specifies the collection data

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report