= List =
List is a generic class that can handle most data.
Initializing a ''List'':
import java.util.ArrayList;
import java.util.List;
// Method 1
List
= ArrayList =
''ArrayList'' is derived from ''List''. It can handle mixed primitives (as ''Object'').
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class MyRecord
{
private Integer id;
private String name;
public ArrayList asArray()
{
ArrayList arr = new ArrayList();
arr.add(this.id);
arr.add(this.name);
return arr;
}
public void fromArray(ArrayList arr)
{
this.id = (Integer)arr.get(0);
this.name = (String)arr.get(1);
this.created_at = (LocalDateTime)arr.get(2);
this.status = (Boolean)arr.get(3);
}
}
To process each object with the correct type:
ArrayList listOfObjects = new ArrayList();
for(Object obj: listOfObjects) {
if (obj instanceof String) {
// handle String
} else if (obj instanceof Integer) {
// handle Integer
} else {
// handle others
}
}