I sometimes ran into this problem when programming.
Imagine I have a data table with two columns. The first column has rows, the second column has integers.
I want to be able to store each row of a table in a dynamic array. Therefore, each element of the array must contain a string and an integer.
I used to do this by simply breaking each column of the table into two separate ArrayLists, and then when I want to add a row, I would call the add () method once on each ArrayList. To remove, I would call the remove (index) method once on each ArrayList with the same index.
But is there a better way? I know that there are classes like HashMap, but they do not allow duplicate keys. I am looking for something that allows duplicate entries.
I know you can do something like this:
ArrayList<Object[]> myArray = new ArrayList<Object[]>(); myArray.add(new Object[]{"string", 123});
I really don't want that every time I get an element from an array, I should use String and Integer, but maybe this is the only way without creating my own? This seems more confusing to me, and I'd rather use two ArrayLists.
So, is there any Java object like ArrayList where it will work as follows:
ArrayList<String, Integer> myArray = new ArrayList<String, Integer>(); myArray.add("string", 123);
source share