arraylist

Remove multiple elements from ArrayList

一曲冷凌霜 提交于 2019-12-17 22:44:48
问题 I have a bunch of indexes and I want to remove elements at these indexes from an ArrayList . I can't do a simple sequence of remove() s because the elements are shifted after each removal. How do I solve this? 回答1: Sort the indices in descending order and then remove them one by one. If you do that, there's no way a remove will affect any indices that you later want to remove. How you sort them will depend on the collection you are using to store the indices. If it's a list, you can do this:

Why toString() method works differently between Array and ArrayList object in Java

末鹿安然 提交于 2019-12-17 22:41:58
问题 String[] array = {"a","c","b"}; ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); System.out.println(array); System.out.println(list); For list [a, b, c] is output while for array some address is output. When we want to output the array values, we can use Arrays.toString(array); which works just like list . I just wonder why we can't call toString() directly on array to get the values. Isn't it more intuitive and convenient to do so? What results

Java ArrayList Index

不问归期 提交于 2019-12-17 22:11:18
问题 int[] alist = new int [3]; alist.add("apple"); alist.add("banana"); alist.add("orange"); Say that I want to use the second item in the ArrayList. What is the coding in order to get the following output? output: banana 回答1: You have ArrayList all wrong, You can't have an integer array and assign a string value. You cannot do a add() method in an array Rather do this: List<String> alist = new ArrayList<String>(); alist.add("apple"); alist.add("banana"); alist.add("orange"); String value = alist

Java ArrayList of ? extends Interface

时光毁灭记忆、已成空白 提交于 2019-12-17 21:32:06
问题 I have a group of classes that all implement a validation interface which has the method isValid() . I want to put a group of objects--all of different classes--into an ArrayList, loop through them and call isValid() on each. Here's my code Email email = new email(); Address address = new Address(); ArrayList<? extends Validation> myValidationObjects = new ArrayList(); But when I try to do: myValidationObjects.add(email); I get: The method add(capture#2-of ? extends Validation) in the type

Why is Java's AbstractList's removeRange() method protected?

六月ゝ 毕业季﹏ 提交于 2019-12-17 21:27:16
问题 Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected ? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation. Is there some hidden rationale? Seems quite inexplicable to me. 回答1: Yes, because that's not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes. † The OP asks why

StackOverFlowError when initializing constructor for a list

帅比萌擦擦* 提交于 2019-12-17 21:22:52
问题 I'm confused about why I keep getting a java.lang.StackOverFlow error when I try to write a constructor for MyArrayList, a personal version of the arraylist class in java. I know StackOverFlow errors happen when there is a recursive call, but that doesn't appear to be the case for the code I have written? Can anybody help explain why I'm getting this error? This is my code, I've included all of the constructors I have written, but the first MyArrayList() is the one that the error indicates in

Check if ArrayList<String> contains part of a string

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 21:22:50
问题 Say I have an ArrayList: <string1.4> <string2.4> <string3.4> and I wish to return the the first element of the arrayList when I say arrayList.containsSubString('string1'); How could this be done other than iterating through each of the elements of the arrayList and checking if string1 is a substring of that element string? 回答1: The only way I can think of is doing something like: strs.get(strs.indexOf(new Object() { @Override public boolean equals(Object obj) { return obj.toString().contains

Removing element from ArrayList every 500 frames

拟墨画扇 提交于 2019-12-17 21:13:05
问题 I have this arraylist: // Add predators predators = new ArrayList(); for (int i = 0; i < predNum; i++) { Creature predator = new Creature(random(width), random(height), 2); predators.add(predator); } How can the statement be structured so that the last element from the predators arraylist is removed every 500 frames? Does it need a loop of some sort? if (frameCount == 500){ predators.remove(1) } 回答1: If you already have a variable that keeps track of what frame you are on, you can use this if

“Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList?

蓝咒 提交于 2019-12-17 21:08:59
问题 "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" is the main error I get when I compile this method: public static ArrayList<ArrayList<Integer>> createSparseArray(int len, double den) { int counter = 0; ArrayList<Integer> placeHolder = new ArrayList<Integer>(); for (int j = 0; j < len; j++) { double randomNumber = Math.random(); if (randomNumber < den) { counter++; placeHolder.add(j); } } ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>

Adding arrays with same values to HashSet results in duplicate items

核能气质少年 提交于 2019-12-17 20:07:38
问题 I'm trying to create a set of arrays of ints, the thing is that if I try to do: HashSet<int[]> s = new HashSet<int[]>(); int a1[] = {1,2,3}; int a2[] = {1,2,3}; s.add(a1); s.add(a2) System.out.println(s.size()); Then s has two objects, but there should be only one. Note: it doesn't matter if it is HashSet< Integer[]>. It just doesn't work. Now If I try to do this with an ArrayList< Integer>, something like: HashSet<ArrayList<Integer>> s = new HashSet<ArrayList<Integer>>(); ArrayList<Integer>