arraylist

Java ArrayList Contain always return false although it contain the same value

戏子无情 提交于 2019-12-17 09:56:22
问题 This is my Hole Class class Hole { public int a; public int b; Hole(int a, int b) { this.a = a; this.b = b; } So i adding an ArrayList that contain several several hole public void checkPathLoop(int x, int y) { //rough code ArrayList<Hole> leftFlowInnerHole = new ArrayList<>(); //left holes rules leftFlowInnerHole.add(new Hole(0, 1)); leftFlowInnerHole.add(new Hole(1, 5)); leftFlowInnerHole.add(new Hole(5, 4)); leftFlowInnerHole.add(new Hole(0, 4)); when i add Hole userInputHole = new Hole(0

Add object to ArrayList at specified index

无人久伴 提交于 2019-12-17 08:05:14
问题 I think it's a fairly simple question, but I can't figure out how to do this properly. I've got an empty arraylist: ArrayList<object> list = new ArrayList<object>(); I've got some objects I want to add and each object has to be at a certain position. It is necessary however that they can be added in each possible order. When I try this, it doesn't work and I get an IndexOutOfBoundsException : list.add(1, object1) list.add(3, object3) list.add(2, object2) What I have tried is filling the

集合(collection) 1.List

放肆的年华 提交于 2019-12-17 08:02:49
ArrayList 和Vector有什么区别 ArrayList的方法和实现基本上和Vector一样,底层都是数组的实现 但是Vector的方法都是线程安全的,ArrayList没有考虑线程的问题 ArrayList在一些算法上做了优化,效率更高 ArrayList 和 LinkedList 区别 1.ArrayList底层是数组的实现,LinkedList底层是链表的实现 1.1 在java中链表就是自己实现了一个类,在类中记录了前一个和后一个的地址 每次查找都需要找到前一个或者后一个才能往前或者往后找到 2.ArrayList查找速度快,但是删除和插入的速度慢 3.LinkedList删除和插入的速度快,但是查询速度较慢 4.LinkedList有自己独有的addFirst addLast removeLast removeFirst的方法 < E > 范型 基本数据类型的泛型集合,必须使用包装类 ​ List< Integer > list = new ArrayList< Integer >(); 集合长度(List.size()) 数组长度(数组名 .length) List接口的特点 1.都是有序的 2.都有下标 3.都可以重复 增删改查 ArrayList LIst = new ArrayList();   一般初始数组容量为10,每次使用完过后,会new一个新的数组

What is the difference between ArrayList.clear() and ArrayList.removeAll()?

强颜欢笑 提交于 2019-12-17 08:00:14
问题 Assuming that arraylist is defined as ArrayList<String> arraylist , is arraylist.removeAll(arraylist) equivalent to arraylist.clear()? If so, can I assume that the clear() method is more efficient for emptying the array list? Are there any caveats in using arraylist.removeAll(arraylist) instead of arraylist.clear() ? 回答1: The source code for clear() : public void clear() { modCount++; // Let gc do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } The source code for

What is the difference between ArrayList.clear() and ArrayList.removeAll()?

我怕爱的太早我们不能终老 提交于 2019-12-17 07:59:06
问题 Assuming that arraylist is defined as ArrayList<String> arraylist , is arraylist.removeAll(arraylist) equivalent to arraylist.clear()? If so, can I assume that the clear() method is more efficient for emptying the array list? Are there any caveats in using arraylist.removeAll(arraylist) instead of arraylist.clear() ? 回答1: The source code for clear() : public void clear() { modCount++; // Let gc do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } The source code for

Using contains on an ArrayList with integer arrays

冷暖自知 提交于 2019-12-17 07:47:53
问题 I have an ArrayList<int[]> , and I add an array to it. ArrayList<int[]> j = new ArrayList<int[]>(); int[] w = {1,2}; j.add(w); Suppose I want to know if j contains an array that has {1,2} in it without using w , since I will be calling it from another class. So, I create a new array with {1,2} in it... int[] t = {1,2}; return j.contains(t); ...but this would return false even though w was added to the list, and w contains the exact same array as t . Is there a way to use contains such that I

Check if an ArrayList contains every element from another ArrayList (or Collection)

有些话、适合烂在心里 提交于 2019-12-17 07:26:49
问题 There is probably a simple one-liner that I am just not finding here, but this is my question: How do I check if an ArrayList contains all of the objects in another ArrayList? I am looking (if it exists) for something along the lines of: //INCORRECT EXAMPLE: if(one.contains(two)) { return true; } else { return false; } For example: ArrayList one = {1, 2, 3, 4, 5} ArrayList two = {1, 2, 3} --> True ArrayList two = {} --> True ArrayList two = {1, 2, 3, 4, 5} --> True ArrayList two = {1, 5, 2} -

How to Retrieve a List object from the firebase in android

我们两清 提交于 2019-12-17 07:19:56
问题 I am having trouble retrieving a List from the Firebase. I have no trouble storing it, but as soon as I try to cast dataSnapshot.getValue() to ArrayList my app crashes, giving an exception: HashMap cannot be casted to ArrayList But when I tried to cast it to a HashMap, it also crashes, giving exception: ArrayList can't be casted to hashmap Need help please! Here is the code that is creating the problem: Fire.addValueEventListener(new ValueEventListener() { @Override public void onDataChange

How to Retrieve a List object from the firebase in android

ε祈祈猫儿з 提交于 2019-12-17 07:19:17
问题 I am having trouble retrieving a List from the Firebase. I have no trouble storing it, but as soon as I try to cast dataSnapshot.getValue() to ArrayList my app crashes, giving an exception: HashMap cannot be casted to ArrayList But when I tried to cast it to a HashMap, it also crashes, giving exception: ArrayList can't be casted to hashmap Need help please! Here is the code that is creating the problem: Fire.addValueEventListener(new ValueEventListener() { @Override public void onDataChange

Hash Set and Array List performances

孤街浪徒 提交于 2019-12-17 07:16:39
问题 I have implemented a method which simply loops around a set of CSV files that contain data on a number of different module. This then adds the 'moduleName' into a hashSet. (Code shown below) I have used a hashSet as it guarantees no duplicates are inserted instead of an ArrayList which would have to use the contain() method and iterate through the list to check if it is already there. I believe using the hash set has a better performance than an array list. Am I correct in stating that? Also,