1, can automatically enlarge
2, can store different types of data
This is how we solve these two points:
1, when a data is full, we will copy this data into a new array, and this new array capacity is larger than the original array. By expanding the length of the array, that is, the capacity of the collection. So here's how we use this method system.arraycopy
Full notation: public static void Arraycopy (object src, int srcpos, object dest, int destpos, int length)
Parameters
@ src-This is the source array @ srcpos-This is the starting position in the source array @dest-This is the target array @ destpos-This is the starting position in the target data @ length-This is the number of array elements to copy
1234 |
int arr1[] = {0,1,2,3,4,5};int arr2[] = {0,10,20,30,40,50}; System.arraycopy (arr1,0,arr2,1,2);//The result is: ARR2 = [0,0,1,30,40,50]; |
2. The second problem, we just need to declare an array of type Object to be able.
The complete code is as follows:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 1581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971 9819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723 8239 |
package com.ys.collection; public class myarraylist { //for storing data private transient object[] The number of elements in the data = null; //collection private int size = 0; //defines a constant of 10. (later used to define the default collection size) private static final int default_capacity = 10; /*** * with parametric constructors * Specifies the size of the array * @param length */ public myarraylist (int initialcapacity) { if (initialcapacity < 0) { throw new illegalargumentexception ("Illegal collection initial capacity value illegal capacity:" + initialcapacity); }else{ // Instantiate an array this.data = new Object[ initialcapacity]; } } /*** * non-parametric constructor * Specifies that the initial size of the array is 10 */ public myarraylist () { this (default_capacity); } /*** * 1, copy the original array, and expand it by one times * 2 , copies the original array, and expands it by one times and inserts the object at the specified location * @param index * @param obj &Nbsp; */ public void checkincrease (Int index,object obj) { if (Size >= data.length) { //instantiation of a new array object[] NewData = new object[size*2]; if (Index = =-1 && obj == null) { system.arraycopy (data, 0, newdata, 0, size); }else{ //object Copy to be inserted in front of index position &nbsP; system.arraycopy (data, index, NewData, index+1, Size-index); } //assigning the NewData array to the data array data = newdata; newdata = null; } } /*** * gets the size of the array * @return */ Public int getsize () { return this.size; } /*** * Gets the index in the collection based on the element * @param o * @return */ public int indexof (Object o) { if (o == null) { for (int i = 0; i < data.length; i++) if ( Data[i]==null) return i; } else { for (int i = 0; i < Data.length; i++) if ( O.equals (Data[i]) return i; } return -1; } /*** * adding elements at the tail * @param obj * @return */ public boolean add (Object obj) { //Check if expansion is required checkincrease ( -1, null); data[size++] = obj; return true; } /** * determines whether a given index is out of bounds * @param index * @return */ public boolean checkindexout ( Int index) { if (Index > Size | | index < 0) { throw new Indexoutofboundsexception ("The specified index is out of bounds, the collection size is:" +size+ ", the index size you specified is:" +index); } return true; } public boolean add (Int index,Object obj) { //if the given index length is exactly equal to the original array length, add it directly to the tail if (index = = size) { add (obj); } // Checkindexout () If the exception is not thrown, the default index <=size, and index > 0 else if (Checkindexout (index)) { if (Size < Data.length) { system.arraycopy (data, index, data, index+1, Size-index); Data[index] = obj; }else{ //needs expansion checkincrease (index, obj); } size++; } return true; } /*** * to get elements from the index * @param index * @return */ public object get (Int index) { Checkindexout (Index); return data[index]; } /*** * Delete all elements */ Public void removeall () { for (Int i = 0 ; i < data.length; i++) { data[i] = null; & NBsp; } } /*** * Delete elements * @param based on index index * @return */ public object Remove (int Index) { if (index = = size+1) { throw new indexoutofboundsexception ("The specified index is out of bounds, the collection size is:" +size+ ", The index size you specified is: "+index); }else if (Checkindexout (index)) { //Saving Objects object obj = data[index]; if (index = = size) { &Nbsp; data[index] = null; } else{ // Move the back array forward one System.arraycopy (data, index+1, data, Index, Size-index); } size--; return obj; } return null; } /* ** * deletes the specified element, the delete succeeds returns True, and the failure returns false * @param obj * @return */ public boolean remove (Object obj) { for (int i = 0 ; i < size; i++) { if (Obj.equals (data[i)) { remove (i); return true; } } return false; } /*** * Modify the element at the specified location, pass the index, and return to the original data after the modification is completed * @param index * @param obj * @return */ public object change (int index,object obj) { checkindexout (index); object OLDOBJ = Data [Index]; data[index] = obj; return oldObj; } /*** * to see if an element is contained in the collection, and if so, returns true without returning false * @param obj * @return */ public Boolean contain (Object obj) { for (int i = 0 ; I < Data.length; i++) { if (Obj.equals (Data[i])) { return true; } } return false; } public static void main (String [] args) { myArrayList my = new myarraylist (); // System.out.println (my.data.length); my.add (0,3); //system.out.println (My.getsize ()); my.add (0,4); //system.out.println (My.getSize ()); my.add (0,5); //my.removeall (); //my.remove (2); // System.out.println (My.get (2)); system.out.println (My.indexof ( NULL)); system.out.println (My.contain (2)); for (int i = 0 ; i < my.data.length; i++) { system.out.println (My.data[i]); } }} |
JAVA implements ArrayList with arrays