In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the source code of ArrayList and LinkedList". In the daily operation, I believe that many people have doubts about what the source code of ArrayList and LinkedList is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "what is the source code of ArrayList and LinkedList?" Next, please follow the editor to study!
In java, the data structure of collection is widely used, and nothing is more widely used than ArrayList and LinkedList under the List interface.
Let's start with List.
1 public interface List extends Collection {2 / / returns the number of elements in the list collection, or Integer.MAX_VALUE 3 int size () if the number is greater than Integer.MAX_VALUE; 4 5 / / determines whether there are no elements in the set, and returns true 6 boolean isEmpty () if no elements exist; 7 8 / / determines whether the specified element o 9 boolean contains (Object o) is contained in the set 10 11 / / returns an iterator 12 Iterator iterator () in the set element in the appropriate sequence; 13 14 / returns an array containing all the elements in the set (in the order from first to last) 15 Object [] toArray (); 16 17 / / puts the elements in the collection into array an and returns 18 T [] toArray (T [] a) 1920 21 / add an element 22 boolean add (E e) to the end of the collection; 23 24 / delete the first occurrence element o25 boolean remove (Object o) from the collection; 26 27 28 / determine whether the collection contains all elements in the specified set c 29 boolean containsAll (Collection c) 30 31 / / appends all elements in the specified collection c to the end of the collection 32 boolean addAll (Collection c); 39 40 / / the intersection of this collection and set c 41 boolean retainAll (Collection c); 42 43 / clears the elements in the collection 44 void clear () 45 46 / / compare whether the specified object o is equal to this collection. Return true47 boolean equals (Object o) only if the specified object is the same list,size size as this collection size and each element equal is the same; 48 49 50 int hashCode (); 51 52 / / returns the element 53 E get (int index) of the specified location index 54 55 / set the element element to the index location of the collection (replace) 56 E set (int index, E element); 57 58 / insert the element element into the collection index location 59 void add (int index, E element); 60 61 / remove the element 62 E remove (int index) of the specified location index; 63 64 / return the first index position 65 int indexOf (Object o) of the specified object o in this collection 66 67 / / returns the last index position 68 int lastIndexOf (Object o) of the specified object o in this collection; 69 70 / / returns an iterator 71 ListIterator listIterator () of ListIterator; 72 73 / / returns a ListInterator iterator 74 ListIterator listIterator (int index) from the specified location index; 75 76 / returns the subset 77 List subList (int fromIndex, int toIndex) from the location fromIndex to the end of the location toIndex; 78}
Let's take a look at how ArrayList,ArrayList can add, delete, modify and search data based on an array.
There are two variables for ArrayList internal maintainers:
/ / the elements in the array survivor collection. The capacity of the collection is the length of the array. ElementData is modified with transient, indicating that the array elementData is not within the serialization range when serialization. Private transient Object [] size of the elementData;// collection (number of elements in the collection) private int size
Let's take another look at ArrayList's constructor:
ArrayList (initialCapacity) {(); if (initialCapacity)
< 0) IllegalArgumentException("Illegal Capacity: "+ initialCapacity); .elementData = new Object[initialCapacity];} ArrayList() { (10);} ArrayList(Collection componentType, int length)方法创建一个元素类型为newType.getComponentType() (该方法返回数组中元素的类型)类型的,长度为newLength的数组,这是一个native方法,然后使用System.arraycopy() 这个native方法将original数组中的元素copy到新创建的数组中,并返回该数组。 我们注意 c.toArray might (incorrectly) not return Object[],按理说一个c.toArray()返回的是一个Object[]类型,其getClass()返回的也一定是Object[].class,那为什么还要进行逐个判断呢? 可真实情况真的是这样吗?我们看下面的示例: //定义一个父类Animalpublic class Aniaml {}//定义Animal的子类Personpublic class Person extends Aniaml{ private int id; private String name; private Date birthday; public Person(int id, String name, Date birthday) { this.id = id; this.name = name; this.birthday = birthday; } public static void main(String[] args) { test1(); test2(); test3(); } public static void test1(){ Person[] persons = {new Person(100,"lewis",new Date()),new Person(100,"lewis",new Date())}; //class [Lcom.lewis.guava.Person; Person的数组类型 System.out.println(persons.getClass()); Aniaml[] aniamls = persons; //class [Lcom.lewis.guava.Person; Person的数组类型 System.out.println(aniamls.getClass()); //class com.lewis.guava.Person Person类型 System.out.println(aniamls[0].getClass()); //java.lang.ArrayStoreException animals[]数组中实际存储的是Peron类型,当运行时放入非Person类型时会报错ArrayStoreException aniamls[0] = new Aniaml(); } public static void test2(){ List list = Arrays.asList("abc"); //class java.util.Arrays$ArrayList 由此可见该类型不是ArrayList类型 System.out.println(list.getClass()); Object[] objects = list.toArray(); //class [Ljava.lang.String; 返回的是String数组类型 System.out.println(objects.getClass()); //java.lang.ArrayStoreException: java.lang.Object 当我们将一个Object放入String数组时报错ArrayStoreException objects[0] = new Object(); } public static void test3(){ List dataList = new ArrayList(); dataList.add(""); dataList.add("hehe"); //class java.util.ArrayList System.out.println(dataList.getClass()); Object[] objects1 = dataList.toArray(); //class [Ljava.lang.Object; System.out.println(objects1.getClass()); objects1[0]="adf"; objects1[0]=123; objects1[0]=new Object(); }} 我们由上面test2()方法可知,一个List,调用list.toArray()返回的Object数组的真实类型不一定是Object数组([Ljava.lang.Object;),当我们调用 Object[] objArray = collection.toArray(), objArray并不一定能够存放Object对象,所以上面的源码中对这种情况进行了判断。 我们接着看ArrayList中的方法: add(E),当我们添加数据的时候,会遇到的一个问题就是:当里面的数组满了,没有可用的容量的怎么办? add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; true;} ensureCapacity( minCapacity) { modCount++; oldCapacity = elementData.length; (minCapacity >OldCapacity) {Object oldData [] = elementData; newCapacity = (oldCapacity * 3) / 2 + 1; (newCapacity
< minCapacity) newCapacity = minCapacity; elementData = Arrays.copyOf(elementData, newCapacity); }} add( index, E element) { (index >Size | | index
< 0) IndexOutOfBoundsException( "Index: "+index+", Size: "+size); ensureCapacity(size+1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++;} E set( index, E element) { RangeCheck(index); E oldValue = (E) elementData[index]; elementData[index] = element; oldValue;} RangeCheck( index) { (index >= size) IndexOutOfBoundsException ("Index:" + index+ ", Size:" + size);} addAll (Collection)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.