arraylist

elements of arraylist duplicated

落爺英雄遲暮 提交于 2019-12-25 03:12:38
问题 i have created an arraylist and added elements (string array) to it in a DO While loop. i use the following to add the elements: tempList.add(recordArray); //- recordArray is a String[] //ArrayList<String[]> tempList = new ArrayList<String[]>();// is declared in the activity before onCreate method if i check the array within the DO WHILE loop using following code: aStringArray = tempList.get(index); Log.i(TAG,"aStringArray[0] = " + aStringArray[3]); index++; i get the correct string for each

AsyncTask, CustomBaseAdapter and Listview

爱⌒轻易说出口 提交于 2019-12-25 02:43:45
问题 I have a CustomBaseAdapter with which i fill certain fields: public ArrayList<SearchResults> GetSearchResults(){ ArrayList<SearchResults> results = new ArrayList<SearchResults>(); Document doc = Jsoup.parse(kpn); Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)"); Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)"); SearchResults sr1 = new SearchResults(); for (Element tdFromSecondColumn : tdsFromSecondColumn) { sr1 = new SearchResults(); sr1

数组转ArrayList的正确方式

ⅰ亾dé卋堺 提交于 2019-12-25 02:34:13
一、简介 今天,我们讲讲开发中常见的错误之一 - 数组转ArrayList的正确方式。 二、数组转ArrayList 项目中难免会有数组对象转换为List对象的需求,很多小伙伴们可能会这样写: String[] array = new String[]{"a", "b", "c"}; //数组转List List<String> list = Arrays.asList(array); list.add("d"); System.out.println(list); 很不幸的是,上面的代码运行的话会直接报错: 原因解析,我们看一下Arrays.asList(array);方法的源码,发现返回值为ArrayList,但是这个ArrayList不是java.util.ArrayList,而是Arrays内部的一个私有静态内部类,查看size()可知道它的长度是固定的,不存在所谓的add()添加元素等方法。 public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } ArrayList的构造方法可以接受一个Collection类型的对象,而我们的 java.util.Arrays.ArrayList正好也是它的一个子类,所以将上面的代码优化为: List<String> list = new

Is List<Object> a good replacement for ArrayList?

筅森魡賤 提交于 2019-12-25 02:22:15
问题 ArrayList , which I use in legacy Compact Framework code, does not seem to be available in newfangled (.NET 4.5.1) code. I am storing instances of custom classes in it. What is a good replacement for it - List<Object> , or is there something more suitable? 回答1: As @HighCore mentioned in his comment, you should use the generic form of List, List<T> . If you have several classes defined that you need to include in that List, they probably have common properties, methods. In that case, you can

Java并发编程笔记之CopyOnWriteArrayList源码分析

為{幸葍}努か 提交于 2019-12-25 02:00:11
并发包中并发List只有CopyOnWriteArrayList这一个,CopyOnWriteArrayList是一个线程安全的ArrayList,对其进行修改操作和元素迭代操作都是在底层创建一个拷贝数组(快照)上进行的,也就是写时拷贝策略。 我们首先看一下CopyOnWriteArrayList的类图有哪些属性和方法,如下图所示: 如上,CopyOnWriteArrayList的类图,每个CopyOnWriteArrayList对象里面有一个array数组对象用来存放具体元素,ReentrantLock独占锁对象用来保证同时只有一个线程对array进行修改,这里只要记得ReentrantLock是独占锁,同时只有一个线程可以获取就可以了。 那么问题来了,如果让我们自己去做一个写时拷贝的线程安全的List,我们会怎么做,要考虑哪些要点?    1.list何时初始化,初始化list元素个数为多少,list是有限大小?    2.如何保证线程安全,比如多个线程进行读写时候,如果保证是线程安全的?   3.如何使用迭代器遍历list时候的数据一致性? 那么我们就进入CopyOnWriteArrayList源码去看,看看设计者是如何处理的。 CopyOnWriteArrayList源码分离如下: 1.初始化,首先看一下CopyOnWriteArrayList的无参构造函数

Update and remove GridView items at runtime

为君一笑 提交于 2019-12-25 01:49:46
问题 i am developing a project where i compare the images of two items,So if two items will have same image after clicking these items should be hide. my code is given below and this code encounter a problem. is this a logical error or any other issue? i try to solve the issue but did't resolve.. Please guide me... here is my main Activity. MainActivity.java public class MainActivity extends Activity { Context ctx; int imagesArray[]; ImageAdapter adapter; List<Integer> pictures; boolean flage =

trying to use ArrayList to hold image resources

℡╲_俬逩灬. 提交于 2019-12-25 01:14:05
问题 In my app, I have a bunch of images in my drawable folder which I select at random and display using imageView. I've been told about ArrayList's which can add/remove objects from the list... in order to prevent image repeats , some sample code I used below: // create an array list ArrayList imageHolder = new ArrayList(); int remaining = 10; public void initArrayList(){ // add elements to the array list imageHolder.add((int)R.drawable.child0); imageHolder.add((int)R.drawable.child1);

Android Remove items from ArrayList<List<String>> [duplicate]

耗尽温柔 提交于 2019-12-25 01:08:01
问题 This question already has answers here : Removing object from ArrayList in for each loop (6 answers) Closed 5 years ago . How to remove items from the Arraylist? final ArrayList<List<String>> finalArrayList = null; buf="1,2,3,four,5,six,nine,10"; finalArrayList.add(Arrays.asList(buf.split(","))); for(int i = finalArrayList .size()-1 ; i >0; i--){ for(int i1 = finalArrayList .get(i) .size()-1 ; i1 > 0; i1--){ if(isNumeric(finalArrayList.get(i).get(i1))==true){ Log.d("arra", finalArrayList.get

剑指offer第三题 从尾到头打印链表

旧时模样 提交于 2019-12-25 01:04:46
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。 解题思路:先入栈相当于链表逆序再出栈实现链表从尾到头的顺序输出。 1 /** 2 * public class ListNode { 3 * int val; 4 * ListNode next = null; 5 * 6 * ListNode(int val) { 7 * this.val = val; 8 * } 9 * } 10 * 11 */ 12 import java.util.*; 13 public class Solution { 14 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 15 if(listNode == null){ 16 return new ArrayList(); 17 }else{ 18 ArrayList<Integer> arr = new ArrayList<Integer>(); 19 Stack<Integer> ss = new Stack<Integer>(); 20 while(listNode!=null){ 21 ss.push(Integer.valueOf(listNode.val)); 22 listNode = listNode.next; 23 } 24

2.1 Arraylist 与 LinkedList 异同

删除回忆录丶 提交于 2019-12-24 22:24:17
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Arraylist 与 LinkedList 异同 java集合框架整体结构:https://www.cnblogs.com/bingyimeiling/p/10255037.html java集合特点及选取原则:https://www.cnblogs.com/xyhero/p/9e5e094652ed1dce7a6f0abfcb3e4f3f.html 1.java集合框架结构 分解后结构: java集合框架成员 java集合(容器): 1.collection集合 List:列表接口; set:集合接口; Queue:队列; 2.Map键值对集合 2. Arraylist 与 LinkedList在java集合框架中的位置 从图中可以看出,ArrayList与LinkedList都是List接口的实现类,因此都实现了List的所有未实现的方法,只是实现的方式有所不同,(编程思想: 从中可以看出面向接口的好处, 对于不同的需求就有不同的实现!),而List接口继承了Collection接口,Collection接口又继承了Iterable接口,因此可以看出List同时拥有了Collection与Iterable接口的特性。 3. Arraylist 与 LinkedList 异同 来源: oschina 链接: