arraylist

Java之集合类

倾然丶 夕夏残阳落幕 提交于 2020-01-24 04:57:33
一、集合类简介 数组是很常用的一种的数据结构,我们用它可以满足很多的功能,但是,有时我们会遇到如下这样的问题: 1、我们需要该容器的长度是不确定的。 2、我们需要它能自动排序。 3、我们需要存储以键值对方式存在的数据。 如果遇到上述的情况,数组是很难满足需求的,接下来本章将介绍另一种与数组类似的数据结构——集合类,集合类在Java中有很重要的意义,保存临时数据,管理对象,泛型,Web框架等,很多都大量用到了集合类。 常见的集合类有这些种: 实现Collection接口的:Set、List以及他们的实现类。 实现Map接口的:HashMap及其实现类,我们常用的有Map及其实现类HashMap,HashTable,List、Set及其实现类ArrayList、HashSet,因为集合类是很大的一块内容,我们不方便把它的全部内容写出来,只能慢慢的增加,希望各位读者有自己想法的,踊跃向我提出,我们共同打造精美的博客,供广大编程爱好者学习,下面我我们通过一个图来整体描述一下: 实现Collection接口的类,如Set和List,他们都是单值元素(其实Set内部也是采用的是Map来实现的,只是键值一样,从表面理解,就是单值),不像实现Map接口的类一样,里面存放的是key-value(键值对)形式的数据。这方面就造成他们很多的不同点,如遍历方式,前者只能采用迭代或者循环来取出值

ArrayList源码分析(Java8)

做~自己de王妃 提交于 2020-01-24 02:52:03
ArrayList集合 1、特点: - 底层是Object[] 的数组 - 在内存中是连续的内存空间、并且按照顺序进行存储的 - 允许有空值存在 - 初始化大小为10 *注意: 1、因为是连续的存储空间,所以在查找某个位置数据是很快的,往中间插入数据是很慢的。 2、只有同一种数据类型的数据才可以放在同一个数组当中。 3、在往集合添加数据的时候需要检查集合是否需要扩容,扩容操作对于性能而言消耗是很大的,如果已知数据量多大的情况下,最好指定集合的大小。* 2、构造方法: (1)无参构造方法: public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } (2)指定初始化大小的构造方法: public ArrayList(int initialCapacity) { if (initialCapacity > 0) { // 当初始化大小大于0时: this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) {//当初始化大小等于0时: this.elementData = EMPTY_ELEMENTDATA; } else { //否则参数错误 throw new

java - 并发集合 Vector、synchronizedCollection、CopyOnWriteArrayList之间的区别。

↘锁芯ラ 提交于 2020-01-24 02:15:04
概要 JDK中提供ArrayList集合方便我们对集合内元素进行增删改查,但是ArrayList为了能够在单线程中快速进行操作其设计并不支持多线程进行操作。ArrayList在多线程环境下可能会产生java.util.ConcurrentModificationException异常。而对于我们需要在多线程下操作集合Jdk为我们内置了支持多线程操作的并发集合类供我们使用。 Vector Vector始于JDK1.0版本而ArrayList始于JDK1.2版本。Vector对于每个对集合操作的方法都有synchronized进行修饰,即Vector使用同步方法来实现。并且Vector的扩容量与ArrayList有所区别,ArrayList的默认扩容量为0.5倍,而Vector的默认扩容量则为1倍。 synchronizedCollection 对于Vector使用同步方法来实现锁机制而synchronizedCollection则是使用同步代码块来实现锁机制,但是,因为SynchronizedList只是使用同步代码块包裹了ArrayList的方法,而ArrayList和Vector中同名方法的方法体内容并无太大差异,所以在锁定范围和锁的作用域上两者并无却别。而在锁定的对象区别上,SynchronizedList的同步代码块锁定的是mutex对象,Vector锁定的是this对象

Remove duplicates from a Java List [duplicate]

做~自己de王妃 提交于 2020-01-24 01:59:10
问题 This question already has answers here : Remove duplicates from a list of objects based on property in Java 8 [duplicate] (9 answers) Closed 18 days ago . I want to remove duplicates from a list like bellow List<DataRecord> transactionList =new ArrayList<DataRecord>(); where the DataRecord is a class public class DataRecord { [.....] private String TUN; and the TUN should be unique 回答1: There are two possbile solutions. The first one is to override the equals method. Simply add: public class

每天AC系列(一):三数之和

本秂侑毒 提交于 2020-01-24 00:28:10
1 题目 LeetCode第15题 ,难度中等,题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。 注意:答案中不可以包含重复的三元组。 2 解法 什么也不管先来个O(n3): for(int i=0;i<nums.length;++i) { for(int j=i+1;j<nums.length;++j) { for(int k=j+1;k<nums.length;++k) { if(nums[i]+nums[j]+nums[k] == 0) { ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(nums[i]); arrayList.add(nums[j]); arrayList.add(nums[k]); result.add(arrayList); } } } } well. 3 优化 上面暴力算法的思想就是单纯三个循环,优化的方法可以考虑降低一个循环,使用"双指针"的思想,首先对数组进行排序,然后一开始固定一个数,然后让两个指针一个指向这个数的右区间的起点,一个指向终点,不断计算这三个值的和,根据得出的和移动左指针或者右指针,一共三种情况: 和等于0,同时移动左右指针

Android convert the ArrayList<HashMap<String,String>> mylist to stringarray

北慕城南 提交于 2020-01-23 21:21:04
问题 I have create an android application program..I dont know how to convert the ArrayList<HashMap<String,String>> mylist to string array..pls tell some idea.Thanks in advance 回答1: Create a new ArrayList<String> , traverse the "mylist", append one after one element into newly created list and finally use toArray(T[]) method of newly created list to obtain a string array. 来源: https://stackoverflow.com/questions/7592730/android-convert-the-arraylisthashmapstring-string-mylist-to-stringarray

Android convert the ArrayList<HashMap<String,String>> mylist to stringarray

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-23 21:20:05
问题 I have create an android application program..I dont know how to convert the ArrayList<HashMap<String,String>> mylist to string array..pls tell some idea.Thanks in advance 回答1: Create a new ArrayList<String> , traverse the "mylist", append one after one element into newly created list and finally use toArray(T[]) method of newly created list to obtain a string array. 来源: https://stackoverflow.com/questions/7592730/android-convert-the-arraylisthashmapstring-string-mylist-to-stringarray

ArrayList与LinkList

℡╲_俬逩灬. 提交于 2020-01-23 16:07:18
1 ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构; 2 LinkedList是更快的 比较代码: public class LinkedListDemo2 { public static void main(String []args) { test1(); //test2();打开test1()关闭test2() } public static void test1(){ long l=System.currentTimeMillis();//系统当前毫秒数 try{ List<byte[]>list=new LinkedList<byte[]>(); boolean b=true; while(b) { list.add(new byte[100*1024]);//会导致堆溢出 } }catch(Exception e) { e.printStackTrace(); } finally { System.out.println(System.currentTimeMillis()-l); //后来的毫秒数减去之前的就是运行时间 } } public static void test2(){ long l=System.currentTimeMillis();//系统当前毫秒数 try{ List<byte[]>list=new

java int与integer的区别

主宰稳场 提交于 2020-01-23 10:33:49
java int与integer的区别 int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者而integer 是后者(也就是一个类);因此在类进行初始化时int类的变量初始为0.而Integer的变量则初始化为null. 2.初始化时:   int i =1;Integer i= new Integer(1);(要把integer 当做一个类看);但由于有了自动装箱和拆箱   ( http://www.cnblogs.com/shenliang123/archive/2012/04/16/2451996.html ),   使得对Integer类也可使用:Integer i= 1;       int 是基本数据类型(面向过程留下的痕迹,不过是对java的有益补充),Integer 是一个类,是int的扩展,定义了很多的转换方法   类似的还有:float Float;double Double;string String等,而且还提供了处理 int 类型时非常有用的其他一些常量和方法   举个例子:当需要往ArrayList,HashMap中放东西时,像int,double这种内建类型是放不进去的

ArrayList遍历

◇◆丶佛笑我妖孽 提交于 2020-01-23 04:24:19
遍历ArrayList: ArrayList tableHead = new ArrayList(); tableHead = ReportsProcess.TableHead(baoBiaoXinXi, LabelZhuJian.Text.Trim()); // 遍历ArrayList foreach (object obj in tableHead) { Response.Write(obj.ToString()); } /// <summary> /// 表头设置 /// </summary> /// <param name="baoBiaoXinXi">报表信息表</param> /// <param name="sn">报表主键</param> /// <returns></returns> public static ArrayList TableHead(string baoBiaoXinXi, string sn) { ArrayList tableHead = new ArrayList(); String conn = OracleHelper.DatabaseConnStr; String queryBiaoTou = "SELECT * FROM " + baoBiaoXinXi + " WHERE SN='" + sn+ "' ";