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

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 removeAll()(As defined in AbstractCollection): public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> e = iterator(); while (e.hasNext()) … Read more

Initial size for the ArrayList

You’re confusing the size of the array list with its capacity: the size is the number of elements in the list; the capacity is how many elements the list can potentially accommodate without reallocating its internal structures. When you call new ArrayList<Integer>(10), you are setting the list’s initial capacity, not its size. In other words, … Read more

Save ArrayList to SharedPreferences

After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you read it back, convert it into an ArrayList, sort it if needed and you’re good to go. //Retrieve the values Set<String> set = myScores.getStringSet(“key”, null); //Set the values Set<String> … Read more

How to quickly and conveniently create a one element arraylist [duplicate]

Fixed size List The easiest way, that I know of, is to create a fixed-size single element List with Arrays.asList(T…) like // Returns a List backed by a varargs T. return Arrays.asList(s); Variable size List If it needs vary in size you can construct an ArrayList and the fixed-sizeList like return new ArrayList<String>(Arrays.asList(s)); and (in … Read more

How to convert an ArrayList containing Integers to primitive int array?

If you are using java-8 there’s also another way to do this. int[] arr = list.stream().mapToInt(i -> i).toArray(); What it does is: getting a Stream<Integer> from the list obtaining an IntStream by mapping each element to itself (identity function), unboxing the int value hold by each Integer object (done automatically since Java 5) getting the … Read more

What are the differences between ArrayList and Vector?

Differences Vectors are synchronized, ArrayLists are not. Data Growth Methods Use ArrayLists if there is no specific requirement to use Vectors. Synchronization If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)