In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of Java container class List ArrayList Vector". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java container class List ArrayList Vector what is the role of it?"
List is an interface that declares various methods, not much to say. Take a look at the ArrayList class.
The member variable of the ArrayList class is Object [] elementData,int size;, where the elementData array is used to store the objects added to the ArrayList, and size is the actual number of objects in the list. The ArrayList class is not thread safe.
The implementation of Vector is basically the same as that of ArrayList, except that the Vector class is thread-safe, and its methods all have the synchronized keyword, so ArrayList performs better if thread synchronization is not considered. At present, their internal implementation principles are implemented by using an array of objects. if the number of elements is determined, the efficiency of the array will be used directly.
Simple usage: (followed by data printing results)
Public class ListDemo {/ * @ param args * / public static void main (String [] args) {List list = new ArrayList (); String [] strArr = new String [3]; boolean ret = list.add (""); list.add (new String ("aa")); list.add (null) System.out.println (list.size ()); / / 3 System.out.println (ret); / / true System.out.println (list); / / [, aa, null] System.out.println (strArr); / / [Ljava.lang.String;@1fee6fc System.out.println (strArr.getClass (). GetName ()); / / [Ljava.lang.String System.out.println (list.indexOf ("aa")); / / 1 System.out.println (list.indexOf (null)); / / 2 String str = list.set (1, "ee"); System.out.println (str); / / aa System.out.println (list); / / [, ee, null] String remove = list.remove (0); System.out.println (remove) / / System.out.println (list); / / [ee, null] boolean result = list.remove ("ff"); System.out.println (result); / / false result = list.remove ("ee"); System.out.println (result); / / true System.out.println (list); / / [null]} public ArrayList () {this (10) } public ArrayList (int initialCapacity) {super (); if (initialCapacity
< 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } public boolean add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; } /*移除指定位置元素,注意每次移除数据都会将数组中后面数据移动来填充数组*/ public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved >0) System.arraycopy (elementData, index+1, elementData, index, numMoved); data after elementData [--size] = null; / / index moves forward in turn, assigning a position of * * to 0 and letting gc reclaim the space. Return oldValue;} public void ensureCapacity (int minCapacity) {modCount++;// is a variable that doesn't matter. Int oldCapacity = elementData.length; / / the initial array length if (minCapacity > oldCapacity) {/ / if the number of array objects > the initial array length, the capacity needs to be expanded. Object oldData [] = elementData; int newCapacity = (oldCapacity * 3) / 2 + 1; / / New capacity size if (newCapacity
< minCapacity) newCapacity = minCapacity; /*该方法会创建一个新的对象数组,然后调用 System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));方法将源数组数据拷贝到新数组中。引用更新,指 向新的对象数组。*/ elementData = Arrays.copyOf(elementData, newCapacity); } } /*将对象数组削减到当前元素数目大小,减少存储空间*/ public void trimToSize() { modCount++; int oldCapacity = elementData.length; if (size < oldCapacity) { elementData = Arrays.copyOf(elementData, size); } } /*查找对象***出现的位置,若没有找到,返回-1。由 代码可知,可以在list中加入null对象,并查找到。*/ public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } /*替换指定位置的元素值,返回该位置中old值*/ public E set(int index, E element) { RangeCheck(index); //检查范围 E oldValue = (E) elementData[index]; elementData[index] = element; return oldValue; } /*返回指定位置的值*/ public E get(int index) { RangeCheck(index); return (E) elementData[index]; } private void RangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException ("Index:" + index+ ", Size:" + size);} public int size () {return size;} public boolean isEmpty () {return size = = 0;} at this point, I believe you have a deeper understanding of "what is the role of Java container List ArrayList Vector". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.