arraylist

ArrayList源码解析

我的梦境 提交于 2020-02-27 06:41:50
ArrayList源码解析 数组介绍 ArrayList源码分析 构造方法 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } } public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() !=

ArrayList.ToArray(Type) Or ArrayList.CopyTo(Array)

≡放荡痞女 提交于 2020-02-27 02:45:27
转自: http://blogs.geekdojo.net/richardhsu/archive/2003/11/08/268.aspx I was using ArrayList.CopyTo(Array) to convert arraylist to a type[] array. I thought ArrayList.ToArray(Type) was for intrinsic types (int, string etc.) but forgot that one the facilitites of OOP is that types that we define can be treated as instinsic types for most occasions. Today I came across a code that showed the use of ArrayList.ToArray(Type) for a type defined by the programmer. ... return (Contact[])mContacts.ToArray(typeof(Contact)); ... here mConstacts is of type ArrayList, and Contact is a class. I was doing the

java.util.ConcurrentModificationException 异常问题详解

我的梦境 提交于 2020-02-26 23:44:33
环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修改就会出现java.util.ConcurrentModificationException异常,本文就以ArrayList为例去理解和解决这种异常。 一、单线程情况下问题分析及解决方案 1.1 问题复现 先上一段抛异常的代码。 1 public void test1() { 2 ArrayList<Integer> arrayList = new ArrayList<>(); 3 for (int i = 0; i < 20; i++) { 4 arrayList.add(Integer.valueOf(i)); 5 } 6 7 // 复现方法一 8 Iterator<Integer> iterator = arrayList.iterator(); 9 while (iterator.hasNext()) { 10 Integer integer = iterator.next(); 11 if (integer.intValue() == 5) { 12 arrayList.remove(integer); 13 } 14 } 15 16 // 复现方法二 17 iterator = arrayList.iterator(); 18 for (Integer value :

手撕ArrayList底层,透彻分析源码

蓝咒 提交于 2020-02-26 22:31:47
ArrayList概述 Hello大家好,今天就来介绍一下ArrayList,说到ArrayList,很多人都知道它的底层是使用数组实现的,线程不安全的,说到它的特点,都会说查找快,增删慢,因为面试题大家都是这么背过来的。今天就来说说它的底层源码吧。 ArrayList更准确的说是动态数组去实现的,这里使用动态两字,是为了能够充分体现它的特点。 再者就是ArrayList不是线程安全的,所以效率比较高,但是否这个是绝对的呢?答案是否定的 。 ArrayList底层源码 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ private static final long serialVersionUID = 8683452581122892189L; private static final int DEFAULT_CAPACITY = 10; private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

【C#接口】基础三

喜夏-厌秋 提交于 2020-02-26 20:02:29
# region 数据类型 byte b = 255 ; //byte范围:0-255 short s = - 32768 ; //short范围:-32768-32767 int i = 2 ; long p = 1222222 ; float f = 1.1F ; //单精度的数值后面要加F double d = 1.11 ; //double类型不用加后缀。范围包括以上所有类型的值 decimal de = 1.1 M ; //银行项目用,精度高 string st = "Hello World" ; st = null ; //st中什么都没有 st = "" ; //st中有个字符串,但这个字符串为空字符串 MessageBox . Show ( st ) ; # endregion # region 一元表达式 自增量 自减量 int a = 2 ; -- a ; //a自减1 MessageBox . Show ( a . ToString ( ) ) ; int a1 = 2 ; ++ a1 ; //a1自加1 MessageBox . Show ( a1 . ToString ( ) ) ; int a2 = 2 ; a2 ++ ; //a2自加1 MessageBox . Show ( a2 . ToString ( ) ) ; int a3 = 2 ; a -

LinkedList与ArrayList的区别

扶醉桌前 提交于 2020-02-26 14:43:03
LinkedList与ArrayList的区别: 1 、 因为Array是基于索引(index)的数据结构,它使用索引在数组中搜索和读取数据是很快的,可以直接返回数组中index位置的元素,因此在随机访问集合元素上有较好的性能。Array获取数据的时间复杂度是O(1),但是要插入、删除数据却是开销很大的,因为这需要移动数组中插入位置之后的的所有元素。ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 (LinkedList是双向链表,有next也有previous) 2 、相对于ArrayList,LinkedList的随机访问集合元素时性能较差,因为需要在双向列表中找到要index的位置,再返回;但在插入,删除操作是更快的。因为LinkedList不像ArrayList一样,不需要改变数组的大小,也不需要在数组装满的时候要将所有的数据重新装入一个新的数组,这是ArrayList最坏的一种情况,时间复杂度是O(n),而LinkedList中插入或删除的时间复杂度仅为O(1)。ArrayList在插入数据时还需要更新索引(除了插入数组的尾部)。 3 、LinkedList需要更多的内存,因为ArrayList的每个索引的位置是实际的数据,而LinkedList中的每个节点中存储的是实际的数据和前后节点的位置。 4 、对于随机访问get和set

动态AOP-Spring AOP 基于@AspectJ

狂风中的少年 提交于 2020-02-26 14:22:26
XML配置 aspectj-autoproxy 注册自定义命名空间Bean定义解析器 AopNamespaceHandler public class AopNamespaceHandler extends NamespaceHandlerSupport { public void init() { // In 2.0 XSD as well as in 2.1 XSD. registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser()); registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator()); // Only in 2.0 XSD: moved to context namespace as of 2.1 registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());

Use of contains in Java ArrayList<String>

时光总嘲笑我的痴心妄想 提交于 2020-02-26 10:49:07
问题 If I have an ArrayList of String forming part of a class in Java like so: private ArrayList<String> rssFeedURLs; If I want to use a method in the class containing the above ArrayList, using ArrayList contains to check if a String is contained in this ArrayList, I believe I should be able to do so as follows: if (this.rssFeedURLs.contains(rssFeedURL)) { Where rssFeedURL is a String. Am I right or wrong? 回答1: You are right. ArrayList.contains() tests equals(), not object identity: returns true

100+经典Java面试题及答案解析

情到浓时终转凉″ 提交于 2020-02-26 09:59:53
面向对象编程(OOP) Java是一个支持并发、基于类和面向对象的计算机编程语言。下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改。 代码复用。 增强代码的可靠性和灵活性。 增加代码的可理解性。 面向对象编程有很多重要的特性,比如:封装,继承,多态和抽象。下面的章节我们会逐个分析这些特性。 封装 封装给对象提供了隐藏内部特性和行为的能力。对象提供一些能被其他对象访问的方法来改变它内部的数据。在Java当中,有3种修饰符:public,private和protected。每一种修饰符给其他的位于同一个包或者不同包下面对象赋予了不同的访问权限。 下面列出了使用封装的一些好处: 通过隐藏对象的属性来保护对象内部的状态。 提高了代码的可用性和可维护性,因为对象的行为可以被单独的改变或者是扩展。 禁止对象之间的不良交互提高模块化。 参考这个文档获取更多关于封装的细节和示例。 多态 多态是编程语言给不同的底层数据类型做相同的接口展示的一种能力。一个多态类型上的操作可以应用到其他类型的值上面。 继承 继承给对象提供了从基类获取字段和方法的能力。继承提供了代码的重用行,也可以在不修改类的情况下给现存的类添加新特性。 抽象 抽象是把想法从具体的实例中分离出来的步骤,因此,要根据他们的功能而不是实现细节来创建类。Java支持创建只暴漏接口而不包含方法实现的抽象的类

初学java之ArrayList类

会有一股神秘感。 提交于 2020-02-26 05:24:14
ArrayList是一个可变长度数组,它实现了List接口,因此它也可以包含重复元素和Null元素,也可以任意的访问和修改元素,随着向 ArrayList 中不断添加元素,其容量也自动增长。不过ArrayList是非同步(同步的意思是如果多个线程同时访问一个实例,任何一个线程对实例做了修改之后,其他线程所访问到的实例应该是修改过的最新的实例)的,我们经常使用List list = Collections.synchronizedList(new ArrayList<E>()); 来返回一个支持ArrayList的同步列表。 它有以下几个常用方法: add (int index, E element) 将指定的元素插入此列表中的指定位置。 add ( E e) 将指定的元素添加到此列表的尾部。 addAll ( Collection <? extends E > c) 按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部。 addAll (int index, Collection <? extends E > c) 从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。 clear () 移除此列表中的所有元素。 set (int index, E element)