js implements ArrayList function with example code
1. ArrayList method summary
Construction method summary
ArrayList()
Construct an empty list with an initial capacity of 10.
ArrayList(Collection extends E> c)
Constructs a list containing the elements of the specified collection, arranged in the order in which they are returned by the collection's iterator.
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
Method summary
boolean add(E e)
Adds the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element into this list at the specified position.
boolean addAll(Collection extends E> c)
Adds all elements in the collection to the end of this list in the order of elements returned by the iterator of the specified collection.
boolean addAll(int index, Collection extends E> c)
Starting from the specified position, insert all elements in the specified collection into this list.
void clear()
Remove all elements in this list.
Object clone()
Returns a shallow copy of this ArrayList instance.
boolean contains(Object o)
Returns true if this list contains the specified element.
void ensureCapacity(int minCapacity)
If necessary, increase the capacity of this ArrayList instance to ensure that it can hold at least the number of elements specified by the minimum capacity parameter.
E get(int index)
Returns the element at the specified position in this list.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list contains no element.
boolean isEmpty()
Returns true if there is no element in this list
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or if this list does not Contains an index, returns -1.
E remove(int index)
Remove the element at the specified position in this list.
boolean remove(Object o)
Removes the first occurrence of the specified element in this list (if it exists).
protected void removeRange(int fromIndex, int toIndex)
Removes all elements in the list whose index is between fromIndex (inclusive) and toIndex (exclusive).
E set(int index, E element)
Replace the element at the specified position in this list with the specified element.
int size()
Returns the number of elements in this list.
Object[] toArray()
Returns an array containing all the elements in this list in appropriate order (from first to last element).
Returns an array containing all the elements in this list in appropriate order (from first to last element); the runtime type of the returned array is Specifies the runtime type of the array.
void trimToSize()
Adjust the capacity of this ArrayList instance to the current size of the list.
2.js implements some functions
<html> <script type="text/javascript" src="json.js?1.1.9"></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; } }; //新建一个List var list=new ArrayList(); //增加一个元素 list.add("0").add("1").add("2").add("3"); //增加指定位置 list.add(2,"22222222222"); //删除指定元素 list.removeObj("3"); //删除指定位置元素 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>
Related articles:
Detailed introduction to using C# to describe data structure 3 :ArrayList graphic code

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

You can use the contains() method of the List interface to check whether an object exists in the list. contains() method booleancontains(Objecto) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null?e==null:o.equals(e)). Parameter c - the element whose presence in this list is to be tested. Return Value Returns true if this list contains the specified element. Throws ClassCastException - if the specified element's type is incompatible with this list (optional). NullP

Use java's ArrayList.remove() function to remove elements from an ArrayList. In Java, ArrayList is a commonly used collection class used to store and operate a set of elements. The ArrayList class provides many methods to add, delete, modify, and query elements in the collection. One of the more frequently used methods is remove(), which can remove elements from an ArrayList. The remove() method of ArrayList has two overloaded forms: one

Why is the initial capacity of HashMap 16? When talking about the initialization capacity of ArrayList, we must first review the initialization capacity of HashMap. Taking the Java8 source code as an example, there are two relevant factors in HashMap: initialization capacity and loading factor: /***Thedefaultinitialcapacity-MUSTbeapoweroftwo.*/staticfinalintDEFAULT_INITIAL_CAPACITY=1>1);if(newCapacity-minCapacity0)newCapacity=hugeCapacity

Use Java's ArrayList.clear() function to clear the elements in the ArrayList. In Java programming, ArrayList is a very commonly used data structure that can dynamically store and access elements. However, in some cases, we may need to clear all elements in the ArrayList in order to reuse or free the memory. At this time, you can use the clear() function of ArrayList to achieve it. ArrayList.clear()

Java uses the contains() function of the ArrayList class to determine whether an element exists. ArrayList is a very commonly used data structure in Java programming. It provides a flexible way to store and manipulate a set of data. In addition to simply adding, deleting and accessing elements, ArrayList also provides some useful methods, such as the contains() function, which is used to determine whether an element exists in the ArrayList. contains() function is A

ArrayListisaclassofJavaCollectionFrameworkthatimplementsListInterface.Itisalinearstructurethatstoresandaccesseseachelementsequentially.Itallowsthestorageofduplicateelementshowever,thereareafewapproachesthatmayhelptogetuniquevaluesfromanArrayList.Inth

Use the removeAll() method of the ArrayList class to remove specified elements from an array list in Java. In Java, ArrayList is a very commonly used data structure used to store and manipulate a set of objects. Sometimes we need to remove specific elements from ArrayList. The ArrayList class provides the removeAll() method to help us achieve this requirement. The function of the removeAll() method is to remove the
