forked from Nnamdikeshi/Java2545examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloArrayList.java
More file actions
32 lines (23 loc) · 940 Bytes
/
Copy pathHelloArrayList.java
File metadata and controls
32 lines (23 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package week4_data_structures;
import java.util.ArrayList;
public class HelloArrayList {
public static void main(String[] args) {
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add(6);
myList.add("Data");
myList.add(100);
myList.add("More data");
System.out.println("Item 1 is " + myList.get(1));
System.out.println("Item 3 is " + myList.get(3));
// Storing data in a variable
// Everything in the list is an object
Object ob = myList.get(2);
// If you know you have a String, need to cast it to the correct type
// Awkward and error prone - fix for this in the lab. Ignore for now.
String s = (String)myList.get(0);
// .get() takes the element number as the argument
// This line would cause an error - there's no element 100
// Object error = myList.get(100);
}
}