arraylist

JAVA集合

孤街浪徒 提交于 2019-12-24 10:45:54
集合 集合框架是一个类库的集合,包含实现集合的接口: Collection接口:该接口是最基本的集合接口 List接口:该接口实现了Collection接口。List是有序集合,允许有相同的元素。使用List能够精确地控制每个元素插入的位置,用户能够使用索引来访问List中的元素,与数组类似。 Set接口:该接口也实现了Collection接口。它不能包含重复的元素,SortedSet是升序排列的Set集合。 Map接口:包含键值对,Map不能包含重复的键。SortedMap是一个升序排列的Map集合。 List和Set的区别: Set 接口实例存储的是无序的,不重复的数据。List 接口实例存储的是有序的,可以重复的元素。 Set检索效率低下,删除和插入效率高,插入和删除不会引起元素位置改变 <实现类有HashSet,TreeSet>。 List和数组类似,可以动态增长,根据实际存储的数据的长度自动增长List的长度。查找元素效率高,插入删除效率低,因为会引起其他元素位置改变 <实现类有ArrayList,LinkedList,Vector> 。 常见集合实现类: ArrayList 该类也是实现了List的接口,实现了可变大小的数组,随机访问和遍历元素时,提供更好的性能。该类也是非同步的,在多线程的情况下不要使用。ArrayList 增长当前长度的50%,插入删除效率低。

Convert JSON inside an array list to an ArrayList (Java)

北战南征 提交于 2019-12-24 10:44:25
问题 My output looks like this : { "IssueField1":{ "id":"customfield_10561", "name":"Bug Disclaimer", "type":null, "value":"<div style>...</div>" }, "IssueField2":{ "id":"customfield_13850", "name":"ENV Work Type (DT)", "type":null, "value":null }, . . . "IssueField9":{ "id":"timespent", "name":"Time Spent", "type":"null", "value":"null" } } I want to create an ArrayList and and add all names in it if the value is not null. Any idea how should I do this in Java ? 回答1: Lets say you have the

Java: Printing Arraylist to Output File?

蹲街弑〆低调 提交于 2019-12-24 09:17:43
问题 EDIT: To test these cases, change sort and filter methods to the following: EXAMPLE SORT METHOD: public void sortTitle() { Collections.sort(songs2, SongComparator.byTitle()); // now we have a sorted list } EXAMPLE FILTER METHOD (FOR STRING INPUT): public void filterTitle(String s) { int n = 0; if (n == 0) { n++; for (Song song1 : songs2) { if ((!(((song1.title).contains(s))))) { itemsToRemove.add(song1); } } songs2.removeAll(itemsToRemove); itemsToRemove.clear(); } } EXAMPLE FILTER METHOD

Android abstract arraylist parcelable

这一生的挚爱 提交于 2019-12-24 09:14:59
问题 I have an arraylist of abstract objects(animals) and I'm trying to send and restore this array. The problem is that I need to give a creator when rebuilding the array, at this moment I'm passing a string that I check in the Animal creator and check what kind of animal I need to create. I don't thing this is the most effective, so is there a better way to create a parcelable abstract array? Here is a little demo project that describes the problem. https://github.com/Evertvandenbruel/parcelable

How to Keep ArrayList Value When Changing Activity in Android

心已入冬 提交于 2019-12-24 09:10:01
问题 This is ViewActivity public class ViewActivity extends AppCompatActivity { private ListView lvPerson; private PersonListAdapter adapter; public List<Person> mPersonList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view); lvPerson = (ListView)findViewById(R.id.listView); //Add new person Intent intent = getIntent(); if(intent.hasExtra("add")){ String name = intent.getStringExtra("name");

How to populate and access any element of 3 Dimensional Array List

懵懂的女人 提交于 2019-12-24 09:07:16
问题 I have a requirement to create a 3-Dimensional ArrayList. ArrayList<ArrayList< ArrayList<Object>>> data_Header_Mapper; How can i populate the 3 D arraylist and how i acccess the leaf child ( Object) of 3 Dimensional ArrayList. Lots of thanks in advance for any help. I am familiar working with Vectors , But due to some problems i want to convert the below code of Vectors to ArrayList. Code For Populating 3 D Vectors is as follows. for (int r=0;r<totalRows;r++) // r - Rows { for (int c=0;c

concurrent modification exception on using addall and removeall on same arraylist

不羁岁月 提交于 2019-12-24 08:47:29
问题 I have to remove a sublist from the ArrayList and add the same in begining. I am using below code - for(int i =0 ;i <m ; i++){ List<Integer> t = q.subList(j, k); q.removeAll(t); q.addAll(0, t); } I am getting concurrent exception modification on addAll . I tried creating a copy list from q and then calling addAll on that, still it throws the error on same line. for(int i =0 ;i <m ; i++){ List<Integer> t = q.subList(j, k); q.removeAll(t); ArrayList<Integer> q1= new ArrayList<Integer>(q); q1

Java中ArrayList与HashMap的遍历

喜你入骨 提交于 2019-12-24 08:35:49
1.ArrayList遍历 package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String args[]){    List<String> list = new ArrayList<String>();    list.add("luojiahui");    list.add("luojiafeng");    //方法1    Iterator it1 = list.iterator();    while(it1.hasNext()){    System.out.println(it1.next());    }    //方法2    for(Iterator it2 = list.iterator();it2.hasNext();){    System.out.println(it2.next());    }    //方法3    for(String tmp:list){    System.out.println(tmp);    }    //方法4    for(int i = 0;i < list.size(

Get random object from arraylist specific variable

ε祈祈猫儿з 提交于 2019-12-24 08:31:03
问题 I'm trying to get a random object from an arraylist. The random should be among the object in the arraylist having the variable available as true. Right now it get a random attendant from the whole arraylist. I just need it to specify a limit to attendants being true(variable) this is the line: return myAtt.get(randAtt.nextInt(myAtt.size())); This is the method: public static Attendant askForAtt() { Scanner scanAtt = new Scanner(System.in); Random randAtt = new Random(); //Attendant asnAtt =

How would I put enemies into an ArrayList

我怕爱的太早我们不能终老 提交于 2019-12-24 08:13:21
问题 These are my 2 enemies en = new Enemy(700, 150); en2 = new Enemy (980, 150); I want to have my program contain several enemies, would i just have to recreate them and with unit collision if (d.intersects(r1) && en.visible == true && en.isAlive == false && !p.hitting){ hitmang(hit); p.hitting = true; } if (d.intersects(r2) && en.visible == true && en.isAlive == false && !p.hitting){ hitmang(hit); p.hitting = true; } if (!d.intersects(r1) && !d.intersects(r2)){ p.hitting = false; } Do I have to