Simulate the arraylist operation in C # using JavaScript (learning and sharing)

Source: Internet
Author: User

Recently, I have been reading JavaScript and learned a lot in Javascript advanced programming. However, I would like to thank Uncle Tom for his selfless dedication in the blog Park. His blog on JavaScript has also helped me a lot. At the same time, I would like to thank "Cai" in the blog, who gave me some suggestions in my essay reply! Thank you again.

 

When I learned C #, I used arraylist. I suddenly wondered if I could use JavaScript to simulate arraylist operations in C! The arraylist instance in C # can be added, removed, and searched later. First of all, it shows that some methods of this simulated operation do not consider execution efficiency issues, but they only report a learning attitude to try.

 

Here to shareCodeWhat's wrong with it. Thank you.

 

 

 /*  

* Simulate arraylist in C # using JavaScript
* Heartbleed smoke and rain

* QQ: 909507090

* [Url] www.qhjsw.net [/url]

* Updated on:

*/

Function Arraylist (){

This . Length = 0;

This . Array = New Array ();

// Obtain the value of the specified index
This . Item = Function (Index ){

Return This . Array [Index];

}

// Add a new item
This . Add = Function (Value ){

This . Array [ This . Length] = value;


This . Length ++;

}

// Remove
This . Remove = Function (Value ){

If ( This . Length> = 1 ){

For ( VaR I = 0; I < This . Length; I ++ ){

If ( This . Array [I] = value ){

For ( VaR J = I; j <( This . Length-1); j ++ ){

This . Array [J] = This . Array [J + 1];

}

This . Length --;

This . Array [ This . Length] = Null ;

This . Array. Length --;

Break ;

}

}

}

Else {

This . Length = 0;

}

}
// Insert

This . Insert = Function (Value, index ){

If (Index <0) {Index = 0 ;}


If (( This . Length> = 1) & (index <= This . Length )){

For ( VaR I = This . Length; I> index; I --){

This . Array [I] = This . Array [I-1];

}


This . Array [Index] = value;


This . Length ++;

}

Else {

This . Add (value );

}

}
// Determine whether the specified value exists

This . Exist = Function (Value ){

If ( This . Length> 1 ){

For ( VaR I = 0; I < This . Length; I ++ ){

If ( This . Array [I] = value ){

Return True ;

}

}

}


Return False ;

}

// Clear
This . Clear = Function (){
// Thank you for your suggestion.
This . Array. Length = 0;

This . Length = 0;

}


This . Getarray = Function (){

Return This . Array;

}

// Length
This . Length = Function (){

Return This . Length;

}

// Import
This . Import = Function (Splitstring, splitchar ){

This . Array = splitstring. Split (splitchar );


This . Length = This . Array. length;


}

// Export with the specified separator and return a string
This . Export =Function (Joinchar ){

VaR Strreturn = "";


If ( This . Length> = 1 ){

For ( VaR I = 0; I < This . Length; I ++ ){

Strreturn + = This . Array [I];


If (I <(This . Length-1 )){

Strreturn + = joinchar;

}

}

}

Return Strreturn;


}

}

If you have any suggestions to help me improve, I will learn and correct them. I hope to share and learn with you. Thank you.

 

 

 

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.