Java Learning Essays---tricks Vector,java Essays---vector
Recently there is time, there is time to make Java, the individual think that the language grammar too much, not one by one to learn, whim, hung a struct2 source code, a deep like the sea Ah, see me smallpox sportive, from the simplest start
1 Public Static voidMain (string[] args) {2 3Vector v =NewVector (4);4 5 //adding elements to the vector static array + dynamic expansion6 //adding elements directly using the Add method7V.add ("Test0"); 8V.add ("Test1"); 9V.add ("Test0"); TenV.add ("Test2"); OneV.add ("Test2"); A - //remove an element from the vector -V.remove ("Test0");//Delete the element of the specified content theV.remove (0);//Delete an element by index number - - //get the number of elements already in the vector - intSize =v.size (); +SYSTEM.OUT.PRINTLN ("Size:" +size); - + //traversing elements in a vector A for(inti = 0;i < V.size (); i++){ at System.out.println (V.get (i)); - } -}
The code is very simple, learn the data structure of all know, a simple new search, but we have to go into the understanding, what is the difference between this thing and the array
The constructor is as follows, meaning that you can initialize a capacity number and how much you decide
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) {tenthis (initialcapacity, 0); One }
Let's take a look at the Java constructor is really more powerful than PHP, support for different parameter calls, for PHP would have been an error.
1 /**2 * Constructs an empty vector with the specified initial capacity and3 * capacity increment.4 *5 * @paraminitialcapacity The initial capacity of the vector6 * @paramcapacityincrement The amount by which the capacity is7 * Increased when the vector overflows8 * @throwsIllegalArgumentException If the specified initial capacity9 * is negativeTen */ One PublicVector (intInitialcapacity,intcapacityincrement) { A Super(); - if(Initialcapacity < 0) - Throw NewIllegalArgumentException ("Illegal capacity:" + the initialcapacity); - This. Elementdata =Newobject[initialcapacity]; - This. capacityincrement =capacityincrement; -}
The code is not very simple, simple initialization of an array of objects, even I a high school students to see, notice the second parameter, this is the control array is filled with how to add, it can be understood as a strategy
Let's see how the add element is implemented.
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 Modcou nt++ ; ten Ensurecapacityhelper (Elementcount + 1 ); one elementdata[elementcount++] = e; return true ; }
Synchronized this thing is used when multithreading is secure, preventing multiple threads from working with colleagues
The key is Ensurecapacityhelper this function
1 /**2 * This implements the unsynchronized semantics of ensurecapacity.3 * Synchronized methods in this class can internally call this4 * method for ensuring capacity without incurring the cost of an5 * Extra synchronization.6 *7 * @see#ensureCapacity (int)8 */9 Private voidEnsurecapacityhelper (intmincapacity) {Ten intOldcapacity =elementdata.length; One if(Mincapacity >oldcapacity) { Aobject[] OldData =Elementdata; - intnewcapacity = (capacityincrement > 0)? -(Oldcapacity + capacityincrement): (Oldcapacity * 2); the if(Newcapacity <mincapacity) { -Newcapacity =mincapacity; - } -Elementdata =arrays.copyof (Elementdata, newcapacity); + } -}
It can be understood that the above code is to look at the array is full of no, if full of the dynamic increase, remember we said that parameter, is can be understood as the extension factor, if there is no definition of double increase, it is so simple, seemingly with C language dynamic array like AH
Summarize
The knowledge points we learned above
1. Synchronized synchronous, equivalent to a lock bar
2. arrays.copyof This function is copied from an array into a new array, the new array capacity can be defined by itself
3. Java constructors can support multiple, if you have different parameters for each constructor
4. Vector this thing is no different from an array, except that it can be extended automatically over static arrays.
We're going to be here today.
http://www.bkjia.com/PHPjc/1081561.html www.bkjia.com true http://www.bkjia.com/PHPjc/1081561.html techarticle Java Learning Essays---tricks Vector,java essays---Vector recently more time, there is time to make Java, the individual think that the language grammar too many, do not go to study, whim ...