arraylist

Add element in an ArrayList efficiently at a given index in java

柔情痞子 提交于 2019-12-23 09:25:51
问题 I need to insert a element of type Person (my own defined class) in an ArrayList at index i I know I can use add(int index, E element). But is there any efficient method to do this as in my list it is taking around 1.5 ms on an average (data collected over 1000 insertion and then average). 回答1: If your task is more insertion / deletion intensive, you can always use java.util.LinkedList. ArrayList has a limited size. Every time you add an element, Java ensures that it can fit - so it grows the

For-Each and Pointers in Java [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-23 09:04:12
问题 This question already has answers here : Why does the foreach statement not change the element value? (6 answers) Closed 2 years ago . Ok, so I'm tyring to iterate through an ArrayList and remove a specefic element. However, I am having some trouble using the For-Each like structure. When I run the following code: ArrayList<String> arr = new ArrayList<String>(); //... fill with some values (doesn't really matter) for(String t : arr) { t = " some other value "; //hoping this would change the

我的ArrayList源码理解

送分小仙女□ 提交于 2019-12-23 09:03:55
ArrayList源码理解 ArrayList底层使用 动态数组 并且 默认初始容量为10,空的数组为{} private static final int DEFAULT_CAPACITY = 10 ; transient Object [ ] elementData ; private static final Object [ ] EMPTY_ELEMENTDATA = { } ; 获取元素、修改元素 // 获取数组下标的元素 public E get ( int index ) { // 检查下标是否越界 rangeCheck ( index ) ; // 返回数组下标的元素 return elementData ( index ) ; } // 修改组数下标元素的值 public E set ( int index , E element ) { // 检查下标是否越界 rangeCheck ( index ) ; // 将修改的值赋值给 旧值 E oldValue = elementData ( index ) ; // 将新值赋值给要修改的值 elementData [ index ] = element ; // 返回旧值 return oldValue ; } 添加元素、按下标添加元素 // 添加元素 public boolean add ( E e ) { //

What type is <?> when making instantiating lists?

萝らか妹 提交于 2019-12-23 08:04:44
问题 I have seen in multiple different places people who instantiate a list or ArrayList like: List<?> l = new ArrayList<>(); What type is ?? Does this mean that it can hold any types in it? If so, why would this be used instead of just and ArrayList? 回答1: Does this mean that it can hold any types in it? No. It means that your l variable could be referring to a list parameterized with any type. So it's actually a restriction : you will not be allowed to add any object to l because you have no idea

Java中常见数据结构:list与map

北慕城南 提交于 2019-12-23 07:56:55
1 1:集合 2 Collection(单列集合) 3 List(有序,可重复) 4 ArrayList 5 底层数据结构是数组,查询快,增删慢 6 线程不安全,效率高 7 Vector 8 底层数据结构是数组,查询快,增删慢 9 线程安全,效率低 10 LinkedList 11 底层数据结构是链表,查询慢,增删快 12 线程不安全,效率高 13 Set(无序,唯一) 14 HashSet 15 底层数据结构是哈希表。 16 哈希表依赖两个方法:hashCode()和equals() 17 执行顺序: 18 首先判断hashCode()值是否相同 19 是:继续执行equals(),看其返回值 20 是true:说明元素重复,不添加 21 是false:就直接添加到集合 22 否:就直接添加到集合 23 最终: 24 自动生成hashCode()和equals()即可 25 26 LinkedHashSet 27 底层数据结构由链表和哈希表组成。 28 由链表保证元素有序。 29 由哈希表保证元素唯一。 30 TreeSet 31 底层数据结构是红黑树。(是一种自平衡的二叉树) 32 如何保证元素唯一性呢? 33 根据比较的返回值是否是0来决定 34 如何保证元素的排序呢? 35 两种方式 36 自然排序(元素具备比较性) 37 让元素所属的类实现Comparable接口 38

PowerShell Remove item [0] from an array

和自甴很熟 提交于 2019-12-23 06:57:07
问题 I'm struggling a bit to remove the first line (item ID) of an array. $test.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array To list all the options I tried ,$test | gm and it clearly states: Remove Method void IList.Remove(System.Object value) RemoveAt Method void IList.RemoveAt(int index) So when I try $test.RemoveAt(0) I get the error: Exception calling "RemoveAt" with "1" argument(s): "Collection was of a fixed size."At line:1 char:1

ArrayList#size() is more than actual number of objects

南笙酒味 提交于 2019-12-23 06:48:42
问题 I have an ArrayList in my Android app, and it has 2 items in it. However, its size method returns 3. I know it sounds really simply and stupid, but here is how it goes: Why would this happen? How can an ArrayList's size return an incorrect number? This causes a null pointer exception when I'm iterating the list. 回答1: IntelliJ has a setting about hiding null values. Right click the variables section in the debugger and choose Customize Data Views... . There is an option there for Hide null

How to check whether an array is sorted or not using is_sorted function in C++?

梦想与她 提交于 2019-12-23 06:35:35
问题 I need to check whether an array is sorted or not using the std::is_sorted() function. I am not sure how I would use begin() and end() so I just passed the array to the function. void sorted(bool value){ if(value) cout << "Array is sorted" << endl; else cout << "Array is not sorted" << endl; } int main(){ int a[10], i; cout << "Enter the sequence" << endl; for(i=0; i<5; i++){ cin >> a[i]; } bool value = is_sorted(a); sorted(value); return 0; } When I do that though I get an error like there

The constructor ArrayAdapter<ArrayList<HashMap<String, String>>>(Context, int, ArrayList<HashMap<String, String>>) is undefined

拟墨画扇 提交于 2019-12-23 05:59:31
问题 I am totally new to Android. This question might be a basic one. But I'm struggling for four days with this. Please Help me. I'm making horizontal listview for providing the contents of several categories from blog. (similar interface with Pulse news app) I got the open source of the horizontal listview and I'm modifying it. This code is CustomArrayAdapter.java . But when I try to write super() ; inside the constructor, it makes an error like this : The constructor ArrayAdapter<ArrayList

Printing an Arraylist in toString with lines separating each entry

放肆的年华 提交于 2019-12-23 05:44:15
问题 I have a toString that needs to print a lot of stuff, including an Arraylist which contains multiple entries. Those entries have to be separated by a new line. Here is the toString code that I am working with right now: @Override public String toString() // Displays the info for a class { return getCourseId() + "\n" + getCourseName() + "\n" + getCourseCode() + "\n" + "\n" + "Instructor" + "\n" + "-------------------------" + "\n" + Instructor.toString() + "\n" + "\n" + "Student Roster" + "\n"