Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:java:lang:arraylist [2017/10/25 08:25] smayr |
swdev:java:lang:arraylist [2017/10/25 08:49] (current) smayr [ArrayList] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | = ArrayList | + | = List = |
+ | List is a generic class that can handle most data. | ||
+ | Initializing a '' | ||
+ | <code java> | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | |||
+ | // Method 1 | ||
+ | List< | ||
+ | 1, " | ||
+ | ); | ||
+ | |||
+ | // Method 2 | ||
+ | List< | ||
+ | arr.add(1); | ||
+ | arr.add(" | ||
+ | arr.add(" | ||
+ | arr.add(1); | ||
+ | arr.add(null); | ||
+ | </ | ||
+ | |||
+ | = ArrayList = | ||
+ | '' | ||
<code java> | <code java> | ||
import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||
Line 21: | Line 43: | ||
} | } | ||
| | ||
- | public void fromArray(List< | + | public void fromArray(ArrayList< |
{ | { | ||
this.id | this.id | ||
Line 29: | Line 51: | ||
} | } | ||
} | } | ||
- | </ | + | </ |
+ | |||
+ | To process each object with the correct type: | ||
+ | <code java> | ||
+ | ArrayList< | ||
+ | for(Object obj: listOfObjects) { | ||
+ | if (obj instanceof String) { | ||
+ | // handle String | ||
+ | } else if (obj instanceof Integer) { | ||
+ | // handle Integer | ||
+ | } else { | ||
+ | // handle others | ||
+ | } | ||
+ | } | ||
+ | </ |