arraylist

Iterator

我与影子孤独终老i 提交于 2020-01-07 18:31:09
迭代对于我们搞Java的来说绝对不陌生。我们常常使用JDK提供的迭代接口进行Java集合的迭代。 Iterator iterator = list.iterator(); while(iterator.hasNext()){ String string = iterator.next(); //do something } 迭代其实我们可以简单地理解为遍历,是一个标准化遍历各类容器里面的所有对象的方法类,它是一个很典型的设计模式。Iterator模式是用于遍历集合类的标准访问方法。它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。 在没有迭代器时我们都是这么进行处理的。如下: 对于数组我们是使用下标来进行处理的: int[] arrays = new int[10]; for(int i = 0 ; i < arrays.length ; i++){ int a = arrays[i]; //do something } 对于ArrayList是这么处理的: List<String> list = new ArrayList<String>(); for(int i = 0 ; i < list.size() ; i++){ String string = list.get(i); //do something } 对于这两种方式

集合操作(一)ArrayList,LinkedList源码分析

一世执手 提交于 2020-01-07 17:33:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> ArrayList: 构造函数: ArrayList提供了三种方式的构造器,可以构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。 ArrayList底层是使用一个Object类型的数组来存放数据的。 transient Object[] elementData; // non-private to simplify nested class access size变量代表List实际存放元素的数量 private int size; 不指定ArrayList大小时,默认数组大小为10 private static final int DEFAULT_CAPACITY = 10; 接下来看几个常用的方法: add: 1. public boolean add(E e) { ensureCapacityInternal(size + 1); // 扩容检测 elementData[size++] = e; //新增元素加到末尾 return true; } 2. public void add(int index, E element) { rangeCheckForAdd

Java8 ArrayList的源码分析(一)

牧云@^-^@ 提交于 2020-01-07 16:53:07
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在Java开发中,最常用的集合莫过于ArrayList, Arraylist 提供了方便的crud的api,看似很复杂,但源码其实很简单,但是jdk1.8与以前版本的方法实现还是有一些不同,下面让我们一起看看ArrayList在Java8中是如何实现的。 查看源代码首先要从构造方法开始,通常初始化一个ArrayList通过如下方式 List<Object> list = new ArrayList<Object>(); ArrayList 有三个构造方法: public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } 这是无参构造方法 DEFAULTCAPACITY_EMPTY_ELEMENTDATA 是一个空的Object 数组 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; **elementData **是ArrayList的核心,是一个object的数组,ArrayList所有的数据操作,都是通过数组实现的 transient Object[] elementData; 所以,无参构造方法的ArrayList

数据结构--ArrayList源码摘要

风格不统一 提交于 2020-01-07 15:01:12
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> ArrayList源码 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { ...... /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. */ private transient E[] elementData; /** * The size of the ArrayList (the number of elements it contains). * * @serial */ private int size; ...... } ArrayList 的底层最重要的两个属性:Object 数组和 size 属性。 ArrayList 的底层数组的调整 add方法--ArrayList源码摘要: public boolean add(E o) { ensureCapacity(size + 1)

How to implement nested XML structure into 2 ListViews?

荒凉一梦 提交于 2020-01-07 08:26:11
问题 I am trying to parse an XML file with the following structure: <groups> <group> <id>...</id> <name>...</name> <description>...</description> <actions> <action>...</action> <action>...</action> </actions> </group> <group> <id>...</id> <name>...</name> <description>...</description> <actions> <action>...</action> <action>...</action> <action>...</action> </actions> </group> <group> <id>...</id> <name>...</name> <description>...</description> <actions> <action>...</action> </actions> </group> <

Arraylist and Arrays

故事扮演 提交于 2020-01-07 08:09:15
问题 What is the difference between ArrayList<Object> al = new ArrayList<Object>(100); and Object[] ar = new Object[100]; Is there any difference in the internal implementation i.e. the allocation in the memory? Do both internally reserve 100 slots in the memory? 回答1: An ArrayList<Object> holds a backing Object[] . The backing Array will be "resized" (a new array will be created and the old data will be copied over) if you would possibly overflow the size and additions are possible. List

Marks Program Calculations?

冷暖自知 提交于 2020-01-07 08:02:19
问题 I have to make a marks program that displays a set of marks that a user enters and some calculations (average, maximum, minimum, range, etc.). It should also be able to sort the marks in ascending order. I managed (with help) to display the marks and sort them but I cannot get the program to do the calculations and display them. This is all the code I have so far: ArrayList <Integer> marks = new ArrayList(); private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { Collections

How can I add (+) an integer to an element in a 2-dimensional ArrayList?

♀尐吖头ヾ 提交于 2020-01-07 07:39:18
问题 I'm trying to add 1 to an integer in a 2-dimensional ArrayList. I'm using the set() method with the element + 1 as the second argument, but the "+ 1" isn't working. When I retrieve the element, it defines it as an object, not an integer. How do I get around this? Code: ArrayList<ArrayList> inventoryList = new ArrayList( Arrays.asList(new ArrayList<String>(), new ArrayList<Integer>())); ... (inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1)); Error: Main.java:47: error: bad

issue to pass arraylist from an intent to another

一世执手 提交于 2020-01-07 06:58:35
问题 Before asking here, i first search how to pass an arraylist from an intent to another. Thanks to one of the post made on SO, I thought i found a way to do so. The thing is, when i try to display the elements from the arraylist of my target activity, i only get one of the numerous elements i originally have in my departure activity. To put it in clear, here is my code in my fisrt activity : HashMap map = new HashMap<String,String>(); map.put("key1","value1"); map.put("key2","value2");