I have a class which holds a list inside a list, and its declared as type String, but i no longer know that its a string.
private ArrayList<List<String>> table;
table = new ArrayList<List<String>>();
table.add(new ArrayList<String>()); //is possible
table.add(new ArrayList<Integer>()); //not possible
But now the nested list could either be string or integer. How can i fix that? I also can’t declare Object must either be string or integer
Also can a single Arraylist hold both ints and strings without declaring as objects?
Edit: It seems you guys are debating the right answer. I will insight why i chose Vikdor answer.
My job is to load a csv, and then user types in command to define each column as string or int. My table class has a private instance variable, and has methods which adds the values. Doing it in Vikdor way makes performing operations on table very easy where i want to merge two table or sort. It will also prevent user from inputting non string or int through an exception.
You might be looking for this:
This would result in unchecked code when you retrieve the inner lists, as follows (which obviously is not recommended in production code):
From OP’s comment:
It is better to use a proper CSV processor (like SuperCSV) that would parse directly into a POJO with appropriate data types associated with each column.