arraylist

Performance on Java ArrayList vs LinkedList, pertaining to only Creation/Insertion and sorting

六眼飞鱼酱① 提交于 2020-01-11 10:50:09
问题 Consider the following code: import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; public class testSortingSpeed { public static final int TOTAL_NUMBER = 10000000; public static void main(String[] args) { System.out.println("Creating ArrayList:"); List<Pair<Integer, Integer>> a = new ArrayList<>(); long start = System.currentTimeMillis(); for (int i = 0; i < TOTAL_NUMBER; i++) { Pair<Integer, Integer> p = new Pair<>( (int ) Math.random()

java 集合

牧云@^-^@ 提交于 2020-01-11 10:19:19
java 集合 数据结构总览 Collection Collection 接口主要关注集合的添加,删除,包含 isEmpty : 判断是否没有元素 size : 获取元素个数 add : 添加元素 addAll : 添加给定集合中的所有元素,相当于并集 remove : 删除元素 removeAll : 删除给定集合中的所有元素,相当于差集 removeIf : 删除满足谓词的元素 retainAll : 保留给定集合中的元素,相当于交集 contains : 判断某个元素是否在集合内 containsAll : 判断给定集合中的所有元素是否都在集合内 clear : 清空所有元素 stream : 支持流处理 { Collection < Integer > c = new ArrayList < > ( List . of ( 1 , 2 , 3 , 4 , 5 ) ) ; assertEquals ( c . size ( ) , 5 ) ; assertFalse ( c . isEmpty ( ) ) ; assertTrue ( c . contains ( 3 ) ) ; assertTrue ( c . containsAll ( List . of ( 2 , 4 ) ) ) ; c . clear ( ) ; assertEquals ( c . size (

集合介绍

不羁岁月 提交于 2020-01-11 09:49:08
1 1:集合 2 Collection(单列集合) 3 List(有序,可重复) 4 ArrayList 5 底层数据结构是数组,查询快,增删慢 6 线程不安全,效率高 7 Vector 8 底层数据结构是数组,查询快,增删慢 9 线程安全,效率低 10 LinkedList 11 底层数据结构是链表,查询慢,增删快 12 线程不安全,效率高 13 Set(无序,唯一) 14 HashSet 15 底层数据结构是哈希表。 16 哈希表依赖两个方法:hashCode()和equals() 17 执行顺序: 18 首先判断hashCode()值是否相同 19 是:继续执行equals(),看其返回值 20 是true:说明元素重复,不添加 21 是false:就直接添加到集合 22 否:就直接添加到集合 23 最终: 24 自动生成hashCode()和equals()即可 25 26 LinkedHashSet 27 底层数据结构由链表和哈希表组成。 28 由链表保证元素有序。 29 由哈希表保证元素唯一。 30 TreeSet 31 底层数据结构是红黑树。(是一种自平衡的二叉树) 32 如何保证元素唯一性呢? 33 根据比较的返回值是否是0来决定 34 如何保证元素的排序呢? 35 两种方式 36 自然排序(元素具备比较性) 37 让元素所属的类实现Comparable接口 38

Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1

坚强是说给别人听的谎言 提交于 2020-01-11 09:20:27
问题 I'm attempting to read a certain file in Java and make it into a multidimensional array. Whenever I read a line of code from the script, The console says: Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 I know that this error is caused when the coding can't reach the specific index, but I have no idea how to fix it at the moment. Here is an example of my coding. int x = 1; while (scanner.hasNextLine()) { String line = scanner.nextLine(); //Explode string line String[] Guild

how to use Multithreading With ArrayList in Java

空扰寡人 提交于 2020-01-11 08:03:11
问题 Hello , I have a program that works perfectly , unfortunantly i have some calculs that takes a lot of time , some minutes .. My objectif is to use multithreading to accelerate the parts that take so much time ,. In this Example I give the prototype of the part that i should parallelize public static ArrayList<Object2> createListOfObject2(ArrayList<Object1> mylist) { ArrayList<Object2> listToReturn = new ArrayList<>(); Object2 object2; for (int i = 0; i < mylist.size(); i++) { for (int j = 0;

Android JSONArray to ArrayList

旧时模样 提交于 2020-01-11 07:16:24
问题 I am trying to parse a JSONArray into and ArrayList in my android app. The PHP script correctly retuns the expected results, however the Java fails with a null pointer exception at resultsList.add(map) public void agencySearch(String tsearch) { // Setting the URL for the Search by Town String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php"; // Building parameters for the search List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new

Can ArrayList be used for readonly purpose in multithreaded environment?

断了今生、忘了曾经 提交于 2020-01-11 06:58:09
问题 I have few ArrayList<T> containing user defined objects (e.g. List<Student>, List<Teachers> ). The objects are immutable in nature, i.e. no setters are provided - and also, due to the nature of the problem, "no one" will ever attempt to modify these objects. Once the 'ArrayList' is populated, no further addition/removal of objects is allowed/possible. So List will not dynamically change. With such given condition, can this data structures (i.e. ArraList ) be safely used by multiple threads

Creating arraylist of arrays

ⅰ亾dé卋堺 提交于 2020-01-11 05:34:12
问题 I'm trying to create an array list of string arrays. When I am done, I want the array list to look like this: [0,0], [0,1], [1,0], [1,1,] I have tried to define an array and then add it to the array list. Then re-define an array and add it again. But the array list only seems to contains the last entry. Take a look: String[] t2 = new String[2]; ArrayList<String[]> list2 = new ArrayList<String[]>(); t2[0]="0"; t2[1]="0"; list2.add(t2); t2[0]="0"; t2[1]="1"; list2.add(t2); t2[0]="1"; t2[1]="0";

JDK之List分析

落花浮王杯 提交于 2020-01-11 04:59:42
List在平时的开发当中用的也很多,但是一般都是面向接口编程,所以使用的是List类型,但是都是用ArrayList或者LinkedList进行相关操作。本文章主要讲解JDK源码之ArrayList和LinkedList。 ArrayList底层采用的是数组的形式维护的,主要的方法有add,remove,size,contains,toArray等相关方法。add其实很简单,底层就是增加数组的长度,然后将要加入的元素放进数组。其实现方式方法为:this.elementData[(this.size++)] = paramE,在此操作之前,会增加将数组的长度加1,以放入新加的元素进入到当前数组中,也就实现了List的增加功能。但是,如果要想插入到指定的位置,操作就复杂一些。它会将当前数组向后移动1位,使用的是System.arraycopy复制后面的元素,然后将指定的元素插入。这样的时间就是代价。所以在插入元素比较多的情况下,优先使用Linkedlist,待会会做介绍。remove道理是一样的。不过remove有重载。当为下标int时,它会将指定的元素复制,然后将指定下标的元素设置为null,这样就实现了指定下标删除的功能。但是重载的不一样。他接收Object类型,这样,不管list中存储的是什么类型,都可以进行remove。使用这个方法时,它会搜索符合条件的元素,并取到下标

java提高篇(二一)—–ArrayList

随声附和 提交于 2020-01-11 03:41:03
一、ArrayList概述 ArrayList是实现List接口的动态数组,所谓动态就是它的大小是可变的。实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。 每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。默认初始容量为10。随着ArrayList中元素的增加,它的容量也会不断的自动增长。在每次添加新的元素时,ArrayList都会检查是否需要进行扩容操作,扩容操作带来数据向新数组的重新拷贝,所以如果我们知道具体业务数据量,在构造ArrayList时可以给ArrayList指定一个初始容量,这样就会减少扩容时数据的拷贝问题。当然在添加大量元素前,应用程序也可以使用ensureCapacity操作来增加ArrayList实例的容量,这可以减少递增式再分配的数量。 注意,ArrayList实现不是同步的 。如果多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。所以为了保证同步,最好的办法是在创建时完成,以防止意外对列表进行不同步的访问: List list = Collections.synchronizedList(new ArrayList(...)); 二、ArrayList源码分析