Home Backend Development PHP Tutorial java学习漫笔- 捣蛋vector

java学习漫笔- 捣蛋vector

Jun 13, 2016 pm 12:27 PM
capacity the vector

java学习随笔--- 捣蛋vector

 

最近比较有时间啦,有时间搞下java,个人觉得学这门语言语法太多啦,不一一去学习啦,心血来潮,挂了个struct2的源代码,一入深似海啊,看得我天花缭乱,从最简单的开始吧

 

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {</span><span style="color: #008080;"> 2</span>         <span style="color: #008080;"> 3</span>         Vector v = <span style="color: #0000ff;">new</span> Vector(4<span style="color: #000000;">);</span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span>         <span style="color: #008000;">//</span><span style="color: #008000;">向Vector中添加元素 静态数组+动态扩展</span><span style="color: #008080;"> 6</span>         <span style="color: #008000;">//</span><span style="color: #008000;">使用add方法直接添加元素 </span><span style="color: #008080;"> 7</span>         v.add("Test0"<span style="color: #000000;">); </span><span style="color: #008080;"> 8</span>         v.add("Test1"<span style="color: #000000;">); </span><span style="color: #008080;"> 9</span>         v.add("Test0"<span style="color: #000000;">); </span><span style="color: #008080;">10</span>         v.add("Test2"<span style="color: #000000;">); </span><span style="color: #008080;">11</span>         v.add("Test2"<span style="color: #000000;">);</span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span>         <span style="color: #008000;">//</span><span style="color: #008000;">从Vector中删除元素 </span><span style="color: #008080;">14</span>         v.remove("Test0"); <span style="color: #008000;">//</span><span style="color: #008000;">删除指定内容的元素 </span><span style="color: #008080;">15</span>         v.remove(0); <span style="color: #008000;">//</span><span style="color: #008000;">按照索引号删除元素</span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span>         <span style="color: #008000;">//</span><span style="color: #008000;">获得Vector中已有元素的个数 </span><span style="color: #008080;">18</span>         <span style="color: #0000ff;">int</span> size =<span style="color: #000000;"> v.size(); </span><span style="color: #008080;">19</span>         System.out.println("size:" +<span style="color: #000000;"> size);</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span>         <span style="color: #008000;">//</span><span style="color: #008000;">遍历Vector中的元素 </span><span style="color: #008080;">22</span>         <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = 0;i ){ <span style="color: #008080;">23</span> <span style="color: #000000;">        System.out.println(v.get(i)); </span><span style="color: #008080;">24</span> <span style="color: #000000;">        } </span><span style="color: #008080;">25</span> }
Copy after login

代码很简单啦,学过数据结构的都知道,简单的新增改查啦,不过我们要深入一下了解,这玩意跟数组有什么区别

构造函数如下,意思是说你可以初始化一个容量的数,多少你自己决定

<span style="color: #008080;"> 1</span>  <span style="color: #008000;">/**</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">     * Constructs an empty vector with the specified initial capacity and</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">     * with its capacity increment equal to zero.</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">     *</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@param</span><span style="color: #008000;">   initialCapacity   the initial capacity of the vector</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@throws</span><span style="color: #008000;"> IllegalArgumentException if the specified initial capacity</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">     *         is negative</span><span style="color: #008080;"> 8</span>      <span style="color: #008000;">*/</span><span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">public</span> Vector(<span style="color: #0000ff;">int</span><span style="color: #000000;"> initialCapacity) {</span><span style="color: #008080;">10</span>     <span style="color: #0000ff;">this</span>(initialCapacity, 0<span style="color: #000000;">);</span><span style="color: #008080;">11</span>     }
Copy after login

 

我们接着来看,java的构造函数可真的比php强大,支持不同参数调用,换php的话早就报错啦

<span style="color: #008080;"> 1</span>     <span style="color: #008000;">/**</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">     * Constructs an empty vector with the specified initial capacity and</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">     * capacity increment.</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">     *</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@param</span><span style="color: #008000;">   initialCapacity     the initial capacity of the vector</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@param</span><span style="color: #008000;">   capacityIncrement   the amount by which the capacity is</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">     *                              increased when the vector overflows</span><span style="color: #008080;"> 8</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@throws</span><span style="color: #008000;"> IllegalArgumentException if the specified initial capacity</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">     *         is negative</span><span style="color: #008080;">10</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">public</span> Vector(<span style="color: #0000ff;">int</span> initialCapacity, <span style="color: #0000ff;">int</span><span style="color: #000000;"> capacityIncrement) {</span><span style="color: #008080;">12</span>     <span style="color: #0000ff;">super</span><span style="color: #000000;">();</span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">if</span> (initialCapacity )<span style="color: #008080;">14</span>             <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> IllegalArgumentException("Illegal Capacity: "+<span style="color: #008080;">15</span> <span style="color: #000000;">                                               initialCapacity);</span><span style="color: #008080;">16</span>     <span style="color: #0000ff;">this</span>.elementData = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Object[initialCapacity];</span><span style="color: #008080;">17</span>     <span style="color: #0000ff;">this</span>.capacityIncrement =<span style="color: #000000;"> capacityIncrement;</span><span style="color: #008080;">18</span>     }
Copy after login

代码是不是很简单,简单的初始化一个对象数组,连我一个高中生的看出来啦,注意到第二个参数,这个是控制数组填满了之后要怎么增加,可以理解为一个策略吧

我们来看看添加元素是怎样实现的

<span style="color: #008080;"> 1</span>   <span style="color: #008000;">/**</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">     * Appends the specified element to the end of this Vector.</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">     *</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> e element to be appended to this Vector</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@return</span><span style="color: #008000;"> {</span><span style="color: #808080;">@code</span><span style="color: #008000;"> true} (as specified by {</span><span style="color: #808080;">@link</span><span style="color: #008000;"> Collection#add})</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@since</span><span style="color: #008000;"> 1.2</span><span style="color: #008080;"> 7</span>      <span style="color: #008000;">*/</span><span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">synchronized</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> add(E e) {</span><span style="color: #008080;"> 9</span>     modCount++<span style="color: #000000;">;</span><span style="color: #008080;">10</span>     ensureCapacityHelper(elementCount + 1<span style="color: #000000;">);</span><span style="color: #008080;">11</span>     elementData[elementCount++] =<span style="color: #000000;"> e;</span><span style="color: #008080;">12</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">13</span>     }
Copy after login
<span style="font-size: 14px;">synchronized 这玩意就是多线程安全的时候用的,防止多个线程同事操作</span><br><br><span style="font-size: 14px;">关键是 ensureCapacityHelper  这个函数<br><br></span>
Copy after login
<span style="color: #008080;"> 1</span> <span style="color: #008000;">/**</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">     * This implements the unsynchronized semantics of ensureCapacity.</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">     * Synchronized methods in this class can internally call this</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">     * method for ensuring capacity without incurring the cost of an</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">     * extra synchronization.</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">     *</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">     * </span><span style="color: #808080;">@see</span><span style="color: #008000;"> #ensureCapacity(int)</span><span style="color: #008080;"> 8</span>      <span style="color: #008000;">*/</span><span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> ensureCapacityHelper(<span style="color: #0000ff;">int</span><span style="color: #000000;"> minCapacity) {</span><span style="color: #008080;">10</span>     <span style="color: #0000ff;">int</span> oldCapacity =<span style="color: #000000;"> elementData.length;</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">if</span> (minCapacity ><span style="color: #000000;"> oldCapacity) {</span><span style="color: #008080;">12</span>         Object[] oldData =<span style="color: #000000;"> elementData;</span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">int</span> newCapacity = (capacityIncrement > 0) ?<span style="color: #008080;">14</span>         (oldCapacity + capacityIncrement) : (oldCapacity * 2<span style="color: #000000;">);</span><span style="color: #008080;">15</span>             <span style="color: #0000ff;">if</span> (newCapacity  minCapacity) {<span style="color: #008080;">16</span>         newCapacity =<span style="color: #000000;"> minCapacity;</span><span style="color: #008080;">17</span> <span style="color: #000000;">        }</span><span style="color: #008080;">18</span>             elementData =<span style="color: #000000;"> Arrays.copyOf(elementData, newCapacity);</span><span style="color: #008080;">19</span> <span style="color: #000000;">    }</span><span style="color: #008080;">20</span>     }
Copy after login

 

<span style="font-size: 14px;"><br>可以这么理解吧,上面这段代码就是看看数组满了没有,如果满了就动态的增加,还记得我们上面说的那个参数吗,就是可以理解为扩展因子,如果没有定义的话就double增加,就是这么简单,貌似跟c语言的动态数组好像啊<br><br>总结一下<br><br>上面我们学到的知识点<br><br></span>
Copy after login
1. synchronized  同步用的,相当于一个锁吧
Copy after login
<span><br>2. Arrays.copyOf 这函数是从一个数组复制到一个新数组里面,新数组容量可以自己定义<br><br>3. java 的构造函数可以支持多个,前提你每个构造函数的参数都不同<br><br>4. vector 这东西跟数组没什么区别,只不过它比静态数组可以自动扩展罢了<br>今天就到这里吧</span>
Copy after login
<span><br><br></span>
Copy after login
<span style="font-size: 14px;"><br><br></span>
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

After 2 months, the humanoid robot Walker S can fold clothes After 2 months, the humanoid robot Walker S can fold clothes Apr 03, 2024 am 08:01 AM

Editor of Machine Power Report: Wu Xin The domestic version of the humanoid robot + large model team completed the operation task of complex flexible materials such as folding clothes for the first time. With the unveiling of Figure01, which integrates OpenAI's multi-modal large model, the related progress of domestic peers has been attracting attention. Just yesterday, UBTECH, China's "number one humanoid robot stock", released the first demo of the humanoid robot WalkerS that is deeply integrated with Baidu Wenxin's large model, showing some interesting new features. Now, WalkerS, blessed by Baidu Wenxin’s large model capabilities, looks like this. Like Figure01, WalkerS does not move around, but stands behind a desk to complete a series of tasks. It can follow human commands and fold clothes

What currency is THE? Is THE coin worth investing in? What currency is THE? Is THE coin worth investing in? Feb 21, 2024 pm 03:49 PM

What currency is THE? THE (Tokenized Healthcare Ecosystem) is a digital currency that uses blockchain technology to focus on innovation and reform in the healthcare industry. THE coin's mission is to use blockchain technology to improve the efficiency and transparency of the medical industry and promote more efficient cooperation among all parties, including patients, medical staff, pharmaceutical companies and medical institutions. The Value and Characteristics of THE Coin First of all, THE Coin, as a digital currency, has the advantages of blockchain - decentralization, high security, transparent transactions, etc., allowing participants to trust and rely on this system. Secondly, the uniqueness of THE coin is that it focuses on the medical and health industry, using blockchain technology to transform the traditional medical system and improve

How to check the latest price of The Sandbox coin? How to check the latest price of The Sandbox coin? Mar 05, 2024 am 11:52 AM

How to check the latest price of TheSandbox currency TheSandbox is a decentralized gaming platform built on the Ethereum blockchain. Land, assets and gaming experiences can be purchased using its native token SAND. The steps to check the latest price of SAND are as follows: Choose a reliable price check website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coindesk.com/Binance: https://www.binance.com/ Search on the website or app SAND. View SAND

Clear the vector in Java using the removeAllElements() method of the Vector class Clear the vector in Java using the removeAllElements() method of the Vector class Jul 24, 2023 am 11:45 AM

Use the removeAllElements() method of the Vector class to clear the vector in Java. In Java programming, the Vector class is a dynamic array class that can add elements to the end of the array and automatically resize it. When we use the Vector class to save a large amount of data, sometimes we need to clear all elements in the vector. In this case, we can use the removeAllElements() method of the Vector class to implement the clearing operation. Vector

How to check the latest price of The Graph coin? How to check the latest price of The Graph coin? Mar 05, 2024 am 09:55 AM

How to check the latest price of TheGraph coin? TheGraph is a decentralized protocol designed to provide efficient indexing and query services for blockchain data. The protocol is designed to make it easier for developers to build and launch decentralized applications (dApps), and to provide these applications with convenient access to blockchain data. To check the latest price of TheGraph Coin (GRT), you can follow these steps: Choose a reliable price checking website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coind

Use java's StringBuilder.capacity() function to get the capacity of the string buffer Use java's StringBuilder.capacity() function to get the capacity of the string buffer Jul 24, 2023 am 11:15 AM

StringBuilder is a commonly used string manipulation class in Java. It provides a series of methods to operate on strings. In many scenarios, we need to splice strings, and often need to modify the length of the strings. In order to perform string operations efficiently, Java provides the StringBuilder class to replace the String class. The StringBuilder class internally uses a character array to store the content of the string. This character array has an initial capacity. When the character

Samsung's new foldable screen product exposed, expected to debut in late July Samsung's new foldable screen product exposed, expected to debut in late July Mar 21, 2024 pm 02:16 PM

Samsung plans to launch a new generation of Galaxy Z Fold and Flip 6 series folding screen smartphones in the second half of this year. Recently, Korean media TheElec and &quot;Jiji Weekly e&quot; revealed more details about these two new products. Samsung Galazy Z Fold6 leaked pictures. Source @chunvn8888 According to TheElec, Samsung Electronics’ supply chain manufacturers are expected to start the production of Galaxy Z Fold6 and Flip 6 related components in early May. In contrast, the production of parts for Galaxy Z Fold5 and Flip 5 started in the second half of May last year. This means that this year’s release schedule for the standard version of the Galaxy Z series is about two to three weeks earlier than last year. go

How to use vector's delete function in C How to use vector's delete function in C Feb 18, 2024 am 11:29 AM

The remove usage of vector in C requires specific code examples: vector in C language is a dynamic array whose size can be adjusted at runtime. It is a very commonly used data structure used to store and manipulate multiple objects. In practical applications, we often need to insert new elements into vectors or delete existing elements. This article will introduce in detail the use of vector remove in C language and give corresponding code examples. vector’s remove function prototype: v

See all articles