How to sort a List/ArrayList?
Collections.sort(testList); Collections.reverse(testList); That will do what you want. Remember to import Collections though! Here is the documentation for Collections.
Collections.sort(testList); Collections.reverse(testList); That will do what you want. Remember to import Collections though! Here is the documentation for Collections.
Yes, pretty much. List<T> is a generic class. It supports storing values of a specific type without casting to or from object (which would have incurred boxing/unboxing overhead when T is a value type in the ArrayList case). ArrayList simply stores object references. As a generic collection, List<T> implements the generic IEnumerable<T> interface and can … Read more
If you don’t want duplicates in a Collection, you should consider why you’re using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList: Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set); … Read more
Quite a few problems with your code: On Arrays.asList returning a fixed-size list From the API: Arrays.asList: Returns a fixed-size list backed by the specified array. You can’t add to it; you can’t remove from it. You can’t structurally modify the List. Fix Create a LinkedList, which supports faster remove. List<String> list = new LinkedList<String>(Arrays.asList(split)); … Read more
Either: Foo[] array = list.toArray(new Foo[0]); or: Foo[] array = new Foo[list.size()]; list.toArray(array); // fill the array Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way: List<Integer> list = …; int[] array = new int[list.size()]; for(int i = 0; i < list.size(); i++) array[i] = … Read more
The following is part of the List interface (which ArrayList implements): E e = list.get(list.size() – 1); E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find the whole API documentation here.
Use like this. List<String> stockList = new ArrayList<String>(); stockList.add(“stock1”); stockList.add(“stock2”); String[] stockArr = new String[stockList.size()]; stockArr = stockList.toArray(stockArr); for(String s : stockArr) System.out.println(s);
List<String> list = ..; String[] array = list.toArray(new String[0]); For example: List<String> list = new ArrayList<String>(); //add some stuff list.add(“android”); list.add(“apple”); String[] stringArray = list.toArray(new String[0]); The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, … Read more
It would be simpler if you were to just declare it as a List – does it have to be an ArrayList? List<String> places = Arrays.asList(“Buenos Aires”, “Córdoba”, “La Plata”); Or if you have only one element: List<String> places = Collections.singletonList(“Buenos Aires”); This would mean that places is immutable (trying to change it will cause … Read more
Summary ArrayList with ArrayDeque are preferable in many more use-cases than LinkedList. If you’re not sure — just start with ArrayList. TLDR, in ArrayList accessing an element takes constant time [O(1)] and adding an element takes O(n) time [worst case]. In LinkedList inserting an element takes O(n) time and accessing also takes O(n) time but LinkedList … Read more