arraylist

ArrayList or List declaration in Java

£可爱£侵袭症+ 提交于 2019-12-17 04:21:43
问题 What is the difference between these two declarations? Declaration 1: ArrayList<String> arrayList = new ArrayList<String>(); Declaration 2: List<String> arrayList = new ArrayList<String>(); 回答1: List<String> arrayList = new ArrayList<String>(); Is generic where you want to hide implementation details while returning it to client, at later point of time you may change implementation from ArrayList to LinkedList transparently. This mechanism is useful in cases where you design libraries etc.,

ArrayList initial capacity and IndexOutOfBoundsException [duplicate]

我是研究僧i 提交于 2019-12-17 04:06:33
问题 This question already has answers here : Initial size for the ArrayList (15 answers) Closed 3 years ago . Consider this sample code: List<String> myList = new ArrayList<String>(7); myList.add(5, "Hello"); myList.removeAll(Collections.singleton(null)); System.out.println(myList.size() + " objects:" ); for (String s : myList) { System.out.println("\t" + s); } myList is initialized with an initial capacity of 7, then the next line attempts to add the String "Hello" at position 5. This throws an

Iterator<转>

怎甘沉沦 提交于 2019-12-17 03:47:56
Iterator就是迭代器的意思。 Iterator是一个接口,利用迭代器主要是获取元素,很少删除元素。有三个方法: 1)hasNext():判断是否有更多的元素,如果有返回true。 2)next():在hasNest()的判断下,如果有更多的元素,就返回下一个元素。 3)Remove() 删除所返回的上一个元素。(可选操作)至少要返回一次才能删除元素。也就是说如果上一个元素都没有,那么怎么能删除呢?那不是要出现异常了吗? 具体操作: 1)定义一个Iterator类型的变量it,在集合框架当中Collection提供了一个方法iterator(),可以返回一个迭代器。只要是间接继承或者直接继承Collection的都有这个方法。所以可以用ArrayList的一个对象al去返回一个迭代器。 使用Iterator获得元素: 代码 import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayListTest { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add(new Point(1, 2)); al.add(new Point(5, 2));

In Java how do you sort one list based on another?

十年热恋 提交于 2019-12-17 03:35:09
问题 I've seen several other questions similiar to this one but I haven't really been able to find anything that resolves my problem. My use case is this: user has a list of items initially (listA). They reorder the items and want to persist that order (listB), however, due to restrictions I'm unable persist the order on the backend so I have to sort listA after I retrieve it. So basically, I have 2 ArrayLists (listA and listB). One with the specific order the lists should be in (listB) and the

Custom ListView Android

孤街浪徒 提交于 2019-12-17 03:17:32
问题 I have problem with my custom ListView. SimpleAdapter adapter = new SimpleAdapter(this, this.dh.selectAll(), R.layout.custom_row_view, new String[] {"Icon","Chance","TeamID"}, new int[] {R.id.text1,R.id.text2, R.id.text3}); setListAdapter(adapter); This is my simple adapter. I want put into them icons, can I do that? 回答1: yes you can ... MyList.java: package com.TestActivity; import java.util.ArrayList; import java.util.HashMap; import android.app.ListActivity; import android.os.Bundle;

Distinction between the capacity of an array list and the size of an array

不问归期 提交于 2019-12-17 03:12:11
问题 I read the below snippet in Core Java I book. Allocating an array list as new ArrayList <'Employee>(100) // capacity is 100 is not the same as allocating a new array as new Employee[100] // size is 100 There is an important distinction between the capacity of an array list and the size of an array. If you allocate an array with 100 entries, then the array has 100 slots, ready for use. An array list with a capacity of 100 elements has the potential of holding 100 elements (and, in fact, more

Unnest multiple arrays in parallel

匆匆过客 提交于 2019-12-17 02:52:12
问题 My last question Passing an array to stored to postgres was a bit unclear. Now, to clarify my objective: I want to create an Postgres stored procedure which will accept two input parameters. One will be a list of some amounts like for instance (100, 40.5, 76) and the other one will be list of some invoices ('01-2222-05','01-3333-04','01-4444-08') . After that I want to use these two lists of numbers and characters and do something with them. For example I want to take each amount from this

Unnest multiple arrays in parallel

北战南征 提交于 2019-12-17 02:52:06
问题 My last question Passing an array to stored to postgres was a bit unclear. Now, to clarify my objective: I want to create an Postgres stored procedure which will accept two input parameters. One will be a list of some amounts like for instance (100, 40.5, 76) and the other one will be list of some invoices ('01-2222-05','01-3333-04','01-4444-08') . After that I want to use these two lists of numbers and characters and do something with them. For example I want to take each amount from this

Remove Item from ArrayList

拜拜、爱过 提交于 2019-12-17 02:43:11
问题 I have an ArrayList suppose list , and it has 8 items A-H and now I want to delete 1,3,5 position Item stored in int array from the list how can I do this. I am trying to do this with ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); list.add("G"); list.add("H"); int i[] = {1,3,5}; for (int j = 0; j < i.length; j++) { list.remove(i[j]); } But after first item deleted positioned of array is changed and in

Sorting arraylist in alphabetical order (case insensitive)

别来无恙 提交于 2019-12-17 02:19:18
问题 I have a string arraylist names which contains names of people. I want to sort the arraylist in alphabetical order. ArrayList<String> names = new ArrayList<String>(); names.add("seetha"); names.add("sudhin"); names.add("Swetha"); names.add("Neethu"); names.add("ananya"); names.add("Athira"); names.add("bala"); names.add("Tony"); names.add("Karthika"); names.add("Nithin"); names.add("Vinod"); names.add("jeena"); Collections.sort(names); for(int i=0; i<names.size(); i++) System.out.println