arraylist

Why does the ArrayList implementation use Object[]?

白昼怎懂夜的黑 提交于 2019-12-17 13:54:28
问题 In Java ArrayList<E> implementation base on a array of objects. Can anybody explain me why implementation of ArrayList<E> uses array Object[] for data storage instead of E[] ? What the benefit of using Object[] ? 回答1: In Java, creating an array of a generic type is not straightforward. The simple approach does not compile: public class Container<E> { E[] arr = new E[3]; // ERROR: Cannot create a generic array of E } Replace E with Object , and all is well (at the expense of added complexity

Why does the ArrayList implementation use Object[]?

我是研究僧i 提交于 2019-12-17 13:54:18
问题 In Java ArrayList<E> implementation base on a array of objects. Can anybody explain me why implementation of ArrayList<E> uses array Object[] for data storage instead of E[] ? What the benefit of using Object[] ? 回答1: In Java, creating an array of a generic type is not straightforward. The simple approach does not compile: public class Container<E> { E[] arr = new E[3]; // ERROR: Cannot create a generic array of E } Replace E with Object , and all is well (at the expense of added complexity

Passing ArrayList of string arrays from one activity to another in android

萝らか妹 提交于 2019-12-17 13:24:18
问题 I want to pass an ArrayList of string arrays from one activity to another in Android. How can I use intent or bundle ? Please consider that intent.putStringArrayListExtra is not working in this case, because it is not for string arrays. 回答1: Not sure what you mean by "ArrayList of string arrays" If you have string array then check the below link Passing string array between android activities http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html ArrayList implements Serializable

How to sort ArrayList using Comparator? [duplicate]

早过忘川 提交于 2019-12-17 12:04:31
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

How to sort ArrayList using Comparator? [duplicate]

不问归期 提交于 2019-12-17 12:04:09
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

java arraylist ensureCapacity not working

久未见 提交于 2019-12-17 10:54:10
问题 Either I'm doing this wrong or i'm not understanding how this method works. ArrayList<String> a = new ArrayList<String>(); a.ensureCapacity(200); a.add(190,"test"); System.out.println(a.get(190).toString()); I would have thought that ensureCapacity would let me insert a record with an index up to that value. Is there a different way to do this? I get an IndexOutOfBounds error on the third line. 回答1: No, ensureCapacity doesn't change the logical size of an ArrayList - it changes the capacity ,

Removing object from ArrayList in for each loop

烈酒焚心 提交于 2019-12-17 10:42:29
问题 I would like to remove an object from an ArrayList when I'm done with it, but I can't find way to do it. Trying to remove it like in the sample code below doesn't want to work. How could I get to the iterator of current px object in this loop to remove it? for( Pixel px : pixel){ [...] if(px.y > gHeigh){ pixel.remove(pixel.indexOf(px)); // here is the thing pixel.remove(px); //doesn't work either } } 回答1: You can't, within the enhanced for loop. You have to use the "long-hand" approach: for

Difference between HashMap and ArrayList in Java?

蹲街弑〆低调 提交于 2019-12-17 10:23:23
问题 In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situations we should use ArrayList and which times to use HashMap . What is the major difference between both of them? 回答1: You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So an ArrayList implements the List interface and a HashMap implements the Map interface. So the real question is when do you

ArrayIndexOutOfBoundsException when using the ArrayList's iterator

喜夏-厌秋 提交于 2019-12-17 10:13:20
问题 Right now, I have a program containing a piece of code that looks like this: while (arrayList.iterator().hasNext()) { //value is equal to a String value if( arrayList.iterator().next().equals(value)) { // do something } } Am I doing that right, as far as iterating through the ArrayList goes? The error I am getting is: java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.get(Unknown Source) at main1.endElement(main1.java:244) at com.sun.org.apache.xerces.internal.parsers

Using volley library, inside the onResponse Method the ArrayList have some data but outside the OnResponse method the arraylist is empty

落爺英雄遲暮 提交于 2019-12-17 10:06:57
问题 I am doing the network operation in a model and then return the result, but the arraylist size is zero when I return it but inside the onResponse method the arraylist size is not zero. How to resolve this? public class doInBackground { //i have initialized the arraylist here ArrayList<Contact> arrayList=new ArrayList<>(); String url="http://192.168.10.3/volley/allUser.php"; private Context context; public doInBackground(Context context){ this.context=context; } public ArrayList<Contact>