I have a 2D ArrayList:
List<List<Integer>> list = new ArrayList<List<Integer>>();
And I want to go through a for loop and convert each line of the list to a new ArrayList:
for (int i = 0; i < list.size(); i++) {
ArrayList<Integer> element = list.get(i);
}
This spits out the error
incompatible types: java.lang.Object cannot be converted to java.util.ArrayList
I don't use 2D ArrayLists very often. Say my list is
[[1, 2, 3], [0, 5], [-2], [2, 3, 1]]
How can I assign each block to an ArrayList? I want to first have an ArrayList that is [1, 2, 3], send it to a function, then rinse and repeat for the rest of the list.
get()
method always return object of type Object
class. You have to downcast it :
ArrayList<Integer> element = ( ArrayList<Integer>)list.get(i);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.