arraylist

ArrayList底层实现原理

我与影子孤独终老i 提交于 2020-03-05 12:48:39
ArrayList概述: ArrayList是List接口的可变数组的实现。实现了所有可选列表操作,并允许包括null在内的所有元素。除了实现列表接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。每个ArrayList的实例都有一个容量,该容量是指用来存储列表元素的数组的大小。随着向ArrayList中中不断添加元素,其容量也自动增长。自动增长会带来数据向新数组的重新拷贝,因此,如果可预知数据量的多少,可在构造的ArrayList时指定其容量。在添加大量元素前,应用程序也可以使用的ensureCapacity操作来增加ArrayList的实例的容量,这可以减少递增式再分配的数量。 Arraylist 实现了list接口,底层是使用数组存放数据,实际上操作就是对数组的操作。 ArrayList实现: 1)数组实现      private transient Object [] elementData; 2)构造方法:(3种实现方式)     1)构造默认的初始化容量列表;     2)构造一个指定的初始化容量的空列表;     3)构造一个包含指定collection的元素的列表,这些元素按照collection的迭代器,返回他们的顺序排列的;   Java代码: public ArrayList(){ 这 ( 10 ) } public ArrayList( int

【Java源码】集合类-ArrayList

﹥>﹥吖頭↗ 提交于 2020-03-05 08:53:05
一、类继承关系 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable ArrayList继承AbstractList,也实现了List和RandomAccess(一个空接口,也就是标记接口。),Cloneable(可被克隆), Serializable接口。 二、类属性 //默认容量 private static final int DEFAULT_CAPACITY = 10; //空对象数组(initialCapacity == 0时返回) private static final Object[] EMPTY_ELEMENTDATA = {}; //调用无参构造方法,返回的是该数组 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; //元素数组,保存添加到ArrayList中的元素 transient Object[] elementData; //ArrayList大小 private int size; elementData是transient修饰,所以elementData不能被序列化

Java Or android ConcurrentModificationException异常原因和解决方法

拜拜、爱过 提交于 2020-03-05 07:14:37
ArrayList<Integer> list = new ArrayList<Integer>(); list.add( 2 ); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ Integer integer = iterator.next(); if (integer== 2 ) list.remove(integer); } 运行结果:    从异常信息可以发现,异常出现在checkForComodification()方法中。   我们不忙看checkForComodification()方法的具体实现,我们先根据程序的代码一步一步看ArrayList源码的实现:   首先看ArrayList的iterator()方法的具体实现,查看源码发现在ArrayList的源码中并没有iterator()这个方法,那么很显然这个方法应该是其父类或者实现的接口中的方法,我们在其父类AbstractList中找到了iterator()方法的具体实现,下面是其实现代码: 1 2 3 public Iterator<E> iterator() { return new Itr(); }   从这段代码可以看出返回的是一个指向Itr类型对象的引用,我们接着看Itr的具体实现

Listview refresh without notifyDataSetChange

半世苍凉 提交于 2020-03-05 04:05:30
问题 There is a problem with notifyDataSetChanged. I have a list which retrieves data from sqlite database. The problem is that I know that I have to call notifyDataSetChanged everytime I use add method of list class. I can't understand why my list shows data after addAll() calling without the notifyDataSetChange(). I also tried using add() but result is the same. I need answer because I want to understand very well how notifyDataSetChange() works. Fragment code: public static List<Wherehouse>

Listview refresh without notifyDataSetChange

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-05 04:05:14
问题 There is a problem with notifyDataSetChanged. I have a list which retrieves data from sqlite database. The problem is that I know that I have to call notifyDataSetChanged everytime I use add method of list class. I can't understand why my list shows data after addAll() calling without the notifyDataSetChange(). I also tried using add() but result is the same. I need answer because I want to understand very well how notifyDataSetChange() works. Fragment code: public static List<Wherehouse>

堆积木

微笑、不失礼 提交于 2020-03-05 01:06:17
练习题:堆积木 Description 蒜头君有n块积木,编号分别为1到n。一开始,蒜头把第i块积木放在位置i。蒜头君进行m次操作,每次操作,蒜头把位置b上的积木整体移动到位置a上面。比如1位置的积木是1,2位置的积木是2,那么把位置2的积木移动到位置1后,位置1上的积木从下到上依次为1,2。 Input 第一行输入2个整数n,m(1≤n≤10000,0≤m≤10000)。 接下来m行,每行输入2个整数a,b(1≤a,b≤n),如果a,b相等则本次不需要移动。 Output 输出n行,第i行输出位置i从下到上的积木编号,如果该行没有积木输出一行空行。 每行末尾不要有多余的空格。(感谢尹浛氿同学以身试险!) Sample Input 1 2 2 1 2 1 2 Sample Output 1 1 2 Sample Input 2 4 4 3 1 4 3 2 4 2 2 Sample Output 2 2 4 3 1 Source 计蒜客 import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String [] args){ Scanner input = new Scanner(System.in); int n,m; n=input

Java ArrayList

[亡魂溺海] 提交于 2020-03-05 00:16:33
/** * @author yuanxuan-chen * @date 2020-03-04 12:49 */ import java.util.ArrayList; import java.util.List; public class NP_ArrayList { public static void main(String[] args) { /** * 定义ArrayList and List, list会快些 */ ArrayList<String> arrayList = new ArrayList<String>(); ArrayList<String> list = new ArrayList<String>(); /** * 增加 * 1, 将字符串放在下表为1的地方,其他往后移动 */ list.add("123"); list.add("456"); list.add("789"); list.add(1, "string"); /** * 将list2,加到list1后面 */ List<String> list2 = new ArrayList<>(); list2.add("b"); list2.add("b"); list.addAll(list2); /** * 删除,返回的值为删除的值 */ String r = list.remove(2); /

C#的集合类型及使用技巧

Deadly 提交于 2020-03-04 18:51:01
在日常开发过程中,我们不能避免的要对批量数据处理,这时候就要用到集合。集合总体上分为线性集合和非线性集合。线性集合是指元素具有唯一的前驱和后驱的数据结构类型;非线性集合是指有多个前驱和后驱的数据结构类型,如树和图。我们这里主要讲常用的线性集合,常用的线性集合有数组、ArrayList、List、Hashtable(哈希表)、Dictionary(字典)、Stack(堆栈集合)、Queue(队列集合)等。 一、数组 数组是一个存储相同类型元素的固定大小的顺序集合。数组属于引用类型,它继承System.Array类,System.Array是所有数组的基类。 1、一维数组 一维数组声明方式: //声明一个长度为5的int类型一维数组 int[] numbers = new int[5]; 一维数组的初始化方式有以下三种写法: //一维数组的初始化方式 int[] numbers1 = new int[5] {1,2,3,4,5};//指定大小和元素,使用条件是数组大小必须与元素个数相匹配。 int[] numbers2 = new int[] {1,2,3,4,5};//不指定数组大小,因为编译器会自动统计元素的个数。 int[] numbers3 = { 1, 2, 3, 4, 5 };//这是更简化的形式,直接使用花括号声明和初始化数组。 访问数组元素: 在声明和初始化数组后

Split String into Fixed Number of Words Recursively [Java]

冷暖自知 提交于 2020-03-04 18:43:25
问题 I am attempting to split a String (stored in ArrayList ) into fixed number of words (not characters) recursively. For example, suppose I have the an ArrayList which contains the following two String phrases: ArrayList<String> words = new ArrayList<String>(); words.add("key1 key2 key3 key4 key5 key6 key7"); words.add("key11 key12 key13 key14 key15 key16 key17"); And I want to split into chunks of 5 words ( int desiredListSize = 5; ) - this would produce the following two lists: LIST 1: word1

表达式解析和表达式求导面向对象

百般思念 提交于 2020-03-04 18:32:28
表达式解析和表达式求导 Differential Homework 形式化描述 对表达式做求导 设定的形式化表述 表达式 → \rightarrow → 空白项 [加减 空白项] 项 空白项 | 表达式 加减 空白项 项 空白项 项 → \rightarrow → [加减 空白项] 因子 | 项 空白项 * 空白项 因子 因子 → \rightarrow → 变量因子 | 常数因子 变量因子 → \rightarrow → 幂函数 | 三角函数 常数因子 → \rightarrow → 带符号的整数 幂函数 → \rightarrow → x [空白项 指数] 三角函数 → \rightarrow → sin 空白项 ‘(’ 空白项 x 空白项 ‘)’ [空白项 指数] | cos 空白项 ‘(’ 空白项 x 空白项 ‘)’ [空白项 指数] 指数 → \rightarrow → ** 空白项 带符号的整数 带符号的整数 → \rightarrow → [加减] 允许前导零的整数 允许前导零的整数 → \rightarrow → (0|1|2|…|9){0|1|2|…|9} 空白字符 → \rightarrow → (空格) | \t 空白项 → \rightarrow → {空白字符} 加减 → \rightarrow → + | - 其中{}表示0个、1个或多个,[