java学习漫笔- 捣蛋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> }
代码很简单啦,学过数据结构的都知道,简单的新增改查啦,不过我们要深入一下了解,这玩意跟数组有什么区别
构造函数如下,意思是说你可以初始化一个容量的数,多少你自己决定
<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> }
我们接着来看,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> }
代码是不是很简单,简单的初始化一个对象数组,连我一个高中生的看出来啦,注意到第二个参数,这个是控制数组填满了之后要怎么增加,可以理解为一个策略吧
我们来看看添加元素是怎样实现的
<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> }
<span style="font-size: 14px;">synchronized 这玩意就是多线程安全的时候用的,防止多个线程同事操作</span><br><br><span style="font-size: 14px;">关键是 ensureCapacityHelper 这个函数<br><br></span>
<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> }
<span style="font-size: 14px;"><br>可以这么理解吧,上面这段代码就是看看数组满了没有,如果满了就动态的增加,还记得我们上面说的那个参数吗,就是可以理解为扩展因子,如果没有定义的话就double增加,就是这么简单,貌似跟c语言的动态数组好像啊<br><br>总结一下<br><br>上面我们学到的知识点<br><br></span>
1. synchronized 同步用的,相当于一个锁吧
<span><br>2. Arrays.copyOf 这函数是从一个数组复制到一个新数组里面,新数组容量可以自己定义<br><br>3. java 的构造函数可以支持多个,前提你每个构造函数的参数都不同<br><br>4. vector 这东西跟数组没什么区别,只不过它比静态数组可以自动扩展罢了<br>今天就到这里吧</span>
<span><br><br></span>
<span style="font-size: 14px;"><br><br></span>

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Rédacteur en chef du Machine Power Report : Wu Xin La version domestique de l'équipe robot humanoïde + grand modèle a accompli pour la première fois la tâche d'exploitation de matériaux flexibles complexes tels que le pliage de vêtements. Avec le dévoilement de Figure01, qui intègre le grand modèle multimodal d'OpenAI, les progrès connexes des pairs nationaux ont attiré l'attention. Hier encore, UBTECH, le « stock numéro un de robots humanoïdes » en Chine, a publié la première démo du robot humanoïde WalkerS, profondément intégré au grand modèle de Baidu Wenxin, présentant de nouvelles fonctionnalités intéressantes. Maintenant, WalkerS, bénéficiant des capacités de grands modèles de Baidu Wenxin, ressemble à ceci. Comme la figure 01, WalkerS ne se déplace pas, mais se tient derrière un bureau pour accomplir une série de tâches. Il peut suivre les commandes humaines et plier les vêtements

Quelle monnaie est THE ? THE (Tokenized Healthcare Ecosystem) est une monnaie numérique qui utilise la technologie blockchain pour se concentrer sur l'innovation et la réforme dans le secteur de la santé. La mission de THE coin est d'utiliser la technologie blockchain pour améliorer l'efficacité et la transparence de l'industrie médicale et promouvoir une coopération plus efficace entre toutes les parties, y compris les patients, le personnel médical, les sociétés pharmaceutiques et les institutions médicales. La valeur et les caractéristiques de THE Coin Tout d'abord, THE Coin, en tant que monnaie numérique, présente les avantages de la blockchain - décentralisation, haute sécurité, transactions transparentes, etc., permettant aux participants de faire confiance et de s'appuyer sur ce système. Deuxièmement, le caractère unique de LA pièce est qu'elle se concentre sur l'industrie médicale et de la santé, en utilisant la technologie blockchain pour transformer le système médical traditionnel et améliorer

Comment vérifier le dernier prix de la devise TheSandbox TheSandbox est une plate-forme de jeu décentralisée construite sur la blockchain Ethereum. Les actifs et les expériences de jeu peuvent être achetés à l'aide de son jeton natif SAND. Les étapes pour vérifier le dernier prix de SAND sont les suivantes : Choisissez un site Web ou une application fiable de vérification des prix. Certains sites Web de requête de prix couramment utilisés incluent : CoinMarketCap : https://coinmarketcap.com/Coindesk : https://www.coondesk.com/Binance : https://www.binance.com/ Recherchez sur le site Web ou l'application SAND. Voir SABLE

Utilisez la méthode removeAllElements() de la classe Vector pour effacer le vecteur en Java. En programmation Java, la classe Vector est une classe de tableau dynamique qui peut ajouter des éléments à la fin du tableau et le redimensionner automatiquement. Lorsque nous utilisons la classe Vector pour enregistrer une grande quantité de données, nous devons parfois effacer tous les éléments du vecteur. Dans ce cas, nous pouvons utiliser la méthode removeAllElements() de la classe Vector pour implémenter l'opération de compensation. Vecteur

Comment vérifier le dernier prix de la pièce TheGraph ? TheGraph est un protocole décentralisé conçu pour fournir des services efficaces d'indexation et de requête pour les données blockchain. Le protocole est conçu pour permettre aux développeurs de créer et de lancer plus facilement des applications décentralisées (dApps) et pour fournir à ces applications un accès pratique aux données de la blockchain. Pour vérifier le dernier prix de TheGraph Coin (GRT), vous pouvez suivre ces étapes : Choisissez un site Web ou une application fiable de vérification des prix. Certains sites Web de requête de prix couramment utilisés incluent : CoinMarketCap : https://coinmarketcap.com/Coindesk : https://www.coind

StringBuilder est une classe de manipulation de chaînes couramment utilisée en Java. Elle fournit une série de méthodes pour opérer sur les chaînes. Dans de nombreux scénarios, nous devons épisser des chaînes et devons souvent modifier la longueur des chaînes. Afin d'effectuer efficacement des opérations sur les chaînes, Java fournit la classe StringBuilder pour remplacer la classe String. La classe StringBuilder utilise en interne un tableau de caractères pour stocker le contenu de la chaîne. Ce tableau de caractères a une capacité initiale lorsque le caractère.

Samsung prévoit de lancer une nouvelle génération de smartphones à écran pliable des séries Galaxy Z Fold et Flip 6 au cours du second semestre de cette année. Récemment, les médias coréens TheElec et « Jiji Weekly e » ont révélé plus de détails sur ces deux nouveaux produits. Samsung Galazy Z Fold6 a divulgué des photos. Source @chunvn8888 Selon TheElec, les fabricants de la chaîne d'approvisionnement de Samsung Electronics devraient commencer la production des composants liés au Galaxy Z Fold6 et au Flip 6 début mai. En revanche, la production de pièces pour le Galaxy Z Fold5 et le Flip 5 a commencé le deuxième. la moitié du mois de mai de l'année dernière. Cela signifie que le calendrier de sortie de cette année pour la version standard de la série Galaxy Z est environ deux à trois semaines plus tôt que l'année dernière. aller

Récemment, j'ai lu le livre blanc sur la configuration des ordinateurs de bureau d'entreprise produit par Logitech au premier semestre. Les connaissances et la logique d'achat impliquées dans les périphériques de bureau au niveau de l'entreprise nous ont beaucoup inspirés. Beaucoup de ces nouveaux points de vue sont très appropriés pour être partagés avec les anciens fans de Zhongguancun. Livre blanc de Logitech : Nouvelles réflexions sur l'achat de périphériques de bureau En tant que leader dans le domaine des périphériques de bureau, la force de la marque Logitech et son innovation technologique sont évidentes pour tous. L'importance de l'heure de sortie du livre blanc L'heure de sortie du livre blanc de Logitech coïncide avec la transformation des modèles de bureaux d'entreprise. La popularité des modèles de bureaux hybrides pose de nouveaux défis en matière de marque employeur et d’attraction des talents. Nouvelles tendances en matière d'achat de périphériques de bureau Les précédentes normes d'achat de périphériques de bureau étaient peut-être trop simplistes. Les employés occupant différents postes ont des besoins très différents en matière de claviers, de souris, de casques et de caméras. Perspectives dans le livre blanc de Logitech Logitech White
