Java learning essay --- tamping vector, java Essay --- vector_PHP tutorial

Source: Internet
Author: User
Java learning essay --- tamping vector, java Essay --- vector. Java learning essays --- tricky vector, java essays --- vector has been around for a while recently and has time to develop java. I personally think there are too many syntaxes to learn this language, so I will not study them one by one, the whim of java learning essay --- the trick and the trick vector, the java Essay --- the vector

Recently, I have had time to develop java. I personally think there are too many syntaxes to learn this language, so I don't want to learn them one by one. I am so worried that I am stuck with the source code of struct2, as soon as I got into the sea, I was dazzled. let's start with the simplest one.

1 public static void main (String [] args) {2 3 Vector v = new Vector (4 ); 4 5 // add elements to the Vector static array + dynamic expansion 6 // use the add method to directly add elements 7 v. add ("Test0"); 8 v. add ("Test1"); 9 v. add ("Test0"); 10 v. add ("Test2"); 11 v. add ("Test2"); 12 13 // delete the Element 14 v from the Vector. remove ("Test0"); // delete the element 15 v of the specified content. remove (0); // delete element 16 17 by index number // Obtain the number of existing elements in the Vector 18 int size = v. size (); 19 System. out. println ("size:" + size); 20 21 // traverse the element 22 for (int I = 0; I <v. size (); I ++) {23 System. out. println (v. get (I); 24} 25}

The code is very simple. anyone who has learned the data structure knows that it is simple to add, modify, and query. But let's take a deeper look at the difference between the code and the array.

The constructor is as follows, which means that you can initialize the number of capacity and decide the number of capacity.

 1  /** 2      * Constructs an empty vector with the specified initial capacity and 3      * with its capacity increment equal to zero. 4      * 5      * @param   initialCapacity   the initial capacity of the vector 6      * @throws IllegalArgumentException if the specified initial capacity 7      *         is negative 8      */ 9     public Vector(int initialCapacity) {10     this(initialCapacity, 0);11     }

Let's take a look at the fact that java constructor is really more powerful than php and supports calling different parameters. if you change php, an error has long been reported.

 1     /** 2      * Constructs an empty vector with the specified initial capacity and 3      * capacity increment. 4      * 5      * @param   initialCapacity     the initial capacity of the vector 6      * @param   capacityIncrement   the amount by which the capacity is 7      *                              increased when the vector overflows 8      * @throws IllegalArgumentException if the specified initial capacity 9      *         is negative10      */11     public Vector(int initialCapacity, int capacityIncrement) {12     super();13         if (initialCapacity < 0)14             throw new IllegalArgumentException("Illegal Capacity: "+15                                                initialCapacity);16     this.elementData = new Object[initialCapacity];17     this.capacityIncrement = capacityIncrement;18     }

The code is not very simple, simple initialization of an object array, even a high school student I can see, noticed the second parameter, this is how to increase after the control array is filled up, it can be understood as a policy.

Let's see how to add elements.

 1   /** 2      * Appends the specified element to the end of this Vector. 3      * 4      * @param e element to be appended to this Vector 5      * @return {@code true} (as specified by {@link Collection#add}) 6      * @since 1.2 7      */ 8     public synchronized boolean add(E e) { 9     modCount++;10     ensureCapacityHelper(elementCount + 1);11     elementData[elementCount++] = e;12         return true;13     }
Synchronized is used in multi-thread security to prevent operations by multiple thread colleagues.

The key is the ensureCapacityHelper function.

 1 /** 2      * This implements the unsynchronized semantics of ensureCapacity. 3      * Synchronized methods in this class can internally call this 4      * method for ensuring capacity without incurring the cost of an 5      * extra synchronization. 6      * 7      * @see #ensureCapacity(int) 8      */ 9     private void ensureCapacityHelper(int minCapacity) {10     int oldCapacity = elementData.length;11     if (minCapacity > oldCapacity) {12         Object[] oldData = elementData;13         int newCapacity = (capacityIncrement > 0) ?14         (oldCapacity + capacityIncrement) : (oldCapacity * 2);15             if (newCapacity < minCapacity) {16         newCapacity = minCapacity;17         }18             elementData = Arrays.copyOf(elementData, newCapacity);19     }20     }


The above code shows if the array is full. if it is full, it will be dynamically increased. do you still remember the parameter we mentioned above? it can be understood as an extension factor, if there is no definition, double is added, which is so simple. it seems like a dynamic array in C language.

To sum up

What we learned above

1. synchronized is used for synchronization. it is equivalent to a lock.

2. Arrays. copyOf this function is used to copy data from an array to a new array. the new array capacity can be customized.

3. java constructor supports multiple constructor, provided that the parameters of each constructor are different.

4. there is no difference between vector and array, but it can be automatically expanded than static array.
Let's get here today.




Tips --- tamping vector, java Essay --- vector has been around for a while recently and has time to develop java. I personally think there are too many syntaxes to learn this language, so I will not study them one by one, whim...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.