Js implements the ArrayList function with instance code, jsarraylist

Source: Internet
Author: User
Tags addall

Js implements the ArrayList function with instance code, jsarraylist

1. Summary of the ArrayList Method

Constructor Summary

ArrayList ()
Create an empty list with an initial capacity of 10.
ArrayList (Collection <? Extends E> c)
Construct a list of elements that contain the specified collection. These elements are arranged in the order returned by the collection iterator.
ArrayList (int initialCapacity)
Creates an empty list with the specified initial capacity.
Method Summary
Boolean add (E e)
Add the specified element to the end of the list.
Void add (int index, E element)
Insert the specified element into the specified position in this list.
Boolean addAll (Collection <? Extends E> c)
Add all elements in the collection to the end of the list in the order returned by the iterator of the specified collection.
Boolean addAll (int index, Collection <? Extends E> c)
Inserts all elements in the specified collection into the list starting from the specified position.
Void clear ()
Remove all elements from this list.
Object clone ()
Returns a superficial copy of The ArrayList instance.
Boolean contains (Object o)
If the list contains the specified element, true is returned.
Void ensureCapacity (int minCapacity)
If necessary, increase the capacity of this ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.
E get (int index)
Returns the element at the specified position in the list.
Int indexOf (Object o)
Returns the index of the specified element that appears for the first time in the list, or-1 if the list does not contain the element.
Boolean isEmpty ()
Returns true if the list contains no elements.
Int lastIndexOf (Object o)
Returns the last index of the specified Element in the list, or-1 if the list does not contain an index.
E remove (int index)
Removes an element from the specified position in the list.
Boolean remove (Object o)
Removes the specified element that appears for the first time in the list (if any ).
Protected void removeRange (int fromIndex, int toIndex)
Remove all elements in the list between fromIndex (included) and toIndex (excluded.
E set (int index, E element)
Replace the specified element with the specified position in the list.
Int size ()
Returns the number of elements in the list.
Object [] toArray ()
Returns an array containing all elements in the list in the appropriate order (from the first to the last element.
<T> T [] toArray (T [])
Returns an array containing all elements in the list in an appropriate order (from the first to the last element). returns the runtime type of the array, which is the runtime type of the specified array.
Void trimToSize ()
Adjust the capacity of the ArrayList instance to the current size of the list.

2. js implementation of some functions

Copy codeThe Code is as follows:
<Html>
<Script type = "text/javascript" src = "json. js"> </script>
<Head>
<Script type = "text/javascript">
Function ArrayList (){
This. arr = [],
This. size = function (){
Return this. arr. length;
},
This. add = function (){
If (arguments. length = 1 ){
This. arr. push (arguments [0]);
} Else if (arguments. length> = 2 ){
Var deleteItem = this. arr [arguments [0];
This. arr. splice (arguments [0], 1, arguments [1], deleteItem)
}
Return this;
},
This. get = function (index ){
Return this. arr [index];
},
This. removeIndex = function (index ){
This. arr. splice (index, 1 );
},
This. removeObj = function (obj ){
This. removeIndex (this. indexOf (obj ));
},
This. indexOf = function (obj ){
For (var I = 0; I <this. arr. length; I ++ ){
If (this. arr [I] === obj ){
Return I;
};
}
Return-1;
},
This. isEmpty = function (){
Return this. arr. length = 0;
},
This. clear = function (){
This. arr = [];
},
This. contains = function (obj ){
Return this. indexOf (obj )! =-1;
}

};

// Create a List
Var list = new ArrayList ();
// Add an element
List. add ("0"). add ("1"). add ("2"). add ("3 ");
// Add a specified location
List. add (2, "22222222222 ");
// Delete a specified Element
List. removeObj ("3 ");
// Delete the specified position Element
List. removeIndex (0 );

For (var I = 0; I <list. size (); I ++ ){
Document. writeln (list. get (I ));
}
Document. writeln (list. contains ("2 "))
</Script>
</Head>
<Body>
</Body>

</Html>


How can I read a java ArrayList array as a string in JS Code?

Use the c: foreach label to read data.

How can I use code to implement the arraylist class in java?

Package com. nishizhen. list;

Public interface List {
Public void insert (int I, Object obj) throws Exception;
Public void delete (int I) throws Exception;
Public Object getData (int I) throws Exception;
Public int size ();
Public boolean isEmpty ();
}
Sequence table:
The average number of times that an element needs to be moved to be inserted in an ordered table is n/2. The number of times that an element needs to be moved to be deleted is (n-1)/2, therefore, the time complexity of the sequence table is O (n ).
The implementation of the sequence table is as follows:
Package com. nishizhen. list;

Public class SeqList implements List {
Final int defaultSize = 10;
Int maxSize; // the maximum length of the sequence table.
Int size; // The current length of the linear table.
Object [] listArray; // an array storing linear Table Elements

Public SeqList (int size ){
Initiate (size );
}

Public SeqList (){
Initiate (defaultSize );
}

Public void initiate (int sz ){
MaxSize = sz;
Size = 0;
ListArray = new Object [sz];
}

Public void insert (int I, Object obj) throws Exception {
If (size = maxSize ){
Throw new Exception ("the sequence table is full and no more elements can be inserted. ");
}
If (I <0 | I> maxSize ){
Throw new Exception ("parameter error. ");
}
Else {
For (int j = size; j> = I; j --){
ListArray [j] = listArray [J-1];
}

ListArray [I] = obj;
Size ++;
}
}

Public void delete (int I) throws Exception {
If (size = 0 ){
Throw new Exception ("the sequence table is empty and you cannot delete the element. ");
}

If (I <0 | I> = size ){
Throw new Exception ("parameter error. "); // The subscript of the array cannot be smaller than 0 or greater than size, because the size and its subsequent meta... the remaining full text>

Related Article

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.