How to customize a variable length array

Source: Internet
Author: User
Tags array length

Summary: This article mainly wrote how to customize a variable length array

An array is a form of organizing a number of elements of the same type in an unordered form in the program design, in order to handle them conveniently.

At the beginning of the definition, the length of the array is defined

There are a number of ways to create a new array below two, but the general writing habit chooses the first

1 New // Create a new string array of length 5
1 New STRING[5];  // Create a new string array of length 5

Since the length of the array cannot be changed once it is initialized, we need a different idea.

If I had a basket that could only fit 5 eggs

Now I want to put in more than one egg but the basket can not put what to do!!

With the legendary four-step approach, you can solve this problem.

1. Find a container that can put down 6 eggs!

2. Put five eggs in a later container!

3. Bring the sixth egg into the container!

4. Peel off the previous "basket" label and attach it to the new container!

At this point, I have a basket that can pack 6 eggs! Isn't it easy?

The same is true for arrays

Consider the basket as an array of length 5 whose elements are 1, 2, 3, 4, 5

At this point, there is an element 6 to join

A new array of length 6 is created first

The elements in the original array are then placed in the new array in turn

And then put element 6 into it.

The original basket tag is then torn to the new array

Point the address of the original array to the new array

Must have seen here, the idea should be no problem

The code is implemented as follows:

1  Public classMyArray {2     //defines an array with an initial length of 0, which is used to cache data3     Privatestring[] src =NewString[0];4     /**5 * How to save data6      * @params the element to save7      */8      Public voidAdd (String s) {9         //defines a new array whose length is the length of the original array +1Tenstring[] Dest =NewString[src.length+1]; One         //put the new element into the last one Adest[src.length]=s; -         //place the original data into the new array with the subscript -              for(inti = 0; i<src.length;i++) { thedest[i]=Src[i]; -                  -             } -             //point the original array to the new array +Src=dest; -     } +     /** A * Method of extracting elements according to subscript at      * @paramIndex to extract the subscript of an element -      * @returnreturns the extracted element -      */ -      PublicString Get (intindex) { -         //returns the element corresponding to the subscript -         returnSrc[index]; in     } -      to     /** + * Get the number of elements in a variable array of elements [length] -      * @returnreturns the number of elements the      */ *      Public intsize () { $         //returns the length of an arrayPanax Notoginseng         returnsrc.length; -     } the     /** + * Insert a new element into the specified position A      * @params The new element to insert the      * @paramIndex to insert the position +      */ -      Public voidInsert (String s,intindex) { $         //creates a new array whose length is the original array length plus 1 $string[] Dest =NewString[src.length+1]; -         //puts the specified element in the specified position -dest[index]=s; the         //An element that precedes the specified subscript is placed in a new array -          for(inti=0;i<index;i++) {Wuyidest[i]=Src[i];  the         } -         //The element after the specified subscript is placed in the new array with the subscript plus 1 Wu          for(inti=index+1;i<src.length+1;i++) { -Dest[i]=src[i-1]; About         } $Src=dest; -     } -     /** - * Delete the specified unknown element A      * @paramIndex the subscript of the element to delete +      */ the      -      Public voidDeleteintindex) { $         //creates a new array whose length is minus 1 of the original array length thestring[] Dest =NewString[src.length-1]; the         //specifies that the original array element before the subscript is placed in a new array with its subscript position the          for(inti=0;i<index;i++) { thedest[i]=Src[i]; -         } in         //specifies that the original array element after the subscript corresponds to its subscript position-1 into the new array the          for(inti=index+1;i<src.length;i++) { thedest[i-1]=Src[i]; About         } theSrc=dest; the     } the      +     /** - * Replaces the element of the specified position with the specified element the      * @paramindex The subscript at the specified positionBayi      * @params the element to replace the      * @returnreturns the replaced the      */ -      -      PublicString Replace (intindex,string s) { the         returnsrc[index]=s; the     }     the}

Thanks for reading

How to customize a variable length array

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.