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.