What’s the C++ version of Java’s ArrayList
Use the std::vector class from the standard library.
Use the std::vector class from the standard library.
Since Gson 2.8.0, you can use TypeToken#getParameterized(Type rawType, Type… typeArguments) to create the TypeToken, then getType() should do the trick. For example: TypeToken.getParameterized(ArrayList.class, myClass).getType()
In Java, it is good practice to use interface types rather than concrete classes in APIs. Your problem is that you1 are using ArrayList (probably in lots of places) where you should really be using List. As a result you created problems for yourself with an unnecessary constraint that the list is an ArrayList. This … Read more
A new array is created and the contents of the old one are copied over. That’s all you know at the API level. Quoting from the docs (my emphasis): Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at … Read more
Lists represent a sequential ordering of elements. Maps are used to represent a collection of key / value pairs. While you could use a map as a list, there are some definite downsides of doing so. Maintaining order: A list by definition is ordered. You add items and then you are able to iterate back … Read more
ArrayList has a indexOf() method. Check the API for more, but here’s how it works: private ArrayList<String> _categories; // Initialize all this stuff private int getCategoryPos(String category) { return _categories.indexOf(category); } indexOf() will return exactly what your method returns, fast.
This will give you a list. List<Card> cardsList = Arrays.asList(hand); If you want an arraylist, you can do ArrayList<Card> cardsList = new ArrayList<Card>(Arrays.asList(hand));
Technically, it’s 10, not zero, if you admit for a lazy initialisation of the backing array. See: public boolean add(E e) { ensureCapacityInternal(size + 1); elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } where /** * Default initial capacity. */ … Read more
Am I doing that right, as far as iterating through the Arraylist goes? No: by calling iterator twice in each iteration, you’re getting new iterators all the time. The easiest way to write this loop is using the for-each construct: for (String s : arrayList) if (s.equals(value)) // … As for java.lang.ArrayIndexOutOfBoundsException: -1 You just … Read more
I remember reading many years ago why 1.5 is preferred over two, at least as applied to C++ (this probably doesn’t apply to managed languages, where the runtime system can relocate objects at will). The reasoning is this: Say you start with a 16-byte allocation. When you need more, you allocate 32 bytes, then free … Read more