-
Notifications
You must be signed in to change notification settings - Fork 18
/
ObjectExample.java
82 lines (68 loc) · 2.74 KB
/
ObjectExample.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.javase.genericsexamples;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Juneau
*/
public class ObjectExample {
public static void main(String[] args){
testObject();
testGenerics();
collectionsTests();
}
public static void testObject(){
ObjectContainer myObj = new ObjectContainer();
// store a string
myObj.setObj("Test");
System.out.println("Value of myObj:" + myObj.getObj());
// store an int
myObj.setObj(3);
System.out.println("Value of myObj:" + myObj.getObj());
List objectList = new ArrayList();
objectList.add(myObj);
// We have to cast...and we must be sure to cast the correct type!
// String myStr = (String) ((ObjectContainer)objectList.get(0)).getObj(); // ClassCastException
}
/**
* Utilizing a container that uses generics allows us to store any type
* of data within the object in a type-safe manner
*/
public static void testGenerics(){
GenericContainer<String> stringContainer = new GenericContainer<String>();
stringContainer.setObj(new String("Test"));
//stringContainer.setObj(3); // will not compile...type error
System.out.println("Value of stringContainer :" + stringContainer.getObj());
GenericContainer<Integer> intContainer = new GenericContainer<Integer>();
intContainer.setObj(3);
intContainer.setObj(5);
//intContainer.setObj("Int"); // will not compile
System.out.println("Value of intContainer: " + intContainer.getObj());
}
public static void collectionsTests(){
List myObjList = new ArrayList();
for(int x=0; x <=10; x++){
ObjectContainer myObj = new ObjectContainer();
myObj.setObj("Test" + x);
myObjList.add(myObj);
}
// Get the objects, we need to cast
for(int x=0; x <= myObjList.size()-1; x++){
ObjectContainer obj = (ObjectContainer) myObjList.get(x);
System.out.println("Object Value: " + obj.getObj());
}
List<GenericContainer> genericList = new ArrayList<GenericContainer>();
for(int x=0; x <=10; x++){
GenericContainer<String> myGeneric = new GenericContainer<String>();
myGeneric.setObj(" Generic Test" + x);
genericList.add(myGeneric);
}
// Get the objects, no need to cast
for(int x=0; x <= myObjList.size()-1; x++){
GenericContainer<String> obj = genericList.get(x);
String objectString = obj.getObj();
// Do something with the string...here we will print it
System.out.println(objectString);
}
}
}