error on TestObject TO = (TestObject) i.next();
in
Programming Questions
•
2 years ago
I was testing that if you have one object, if it stays that object if you put it in a ArrayList etc. or if a copy gets made.
So for everything still refers to the original object.
Only for the hasmMap i can't get it to work.
I got a classCastException, why?
- ArrayList<TestObject> TOAL = new ArrayList<TestObject>();
- TestObject[] TOAR = new TestObject[1];
- TestObject[] TOAR2 = new TestObject[1];
- HashMap hm = new HashMap();
- void setup(){
- TOAR[0] = new TestObject();
- TOAR[0].x = 50;
- TOAR[0].y = 50;
- TOAL.add(TOAR[0]);
- TestObject TO = TOAL.get(0);
- hm.put("a", TO);
- TO.x = 25;
- TO.y = 25;
- TOAR[0].x = 75;
- TOAR2[0] = TOAR[0];
- }
- void draw(){
- // point(TOAR2[0].x, TOAR[0].y);
- Iterator i = hm.entrySet().iterator();
- while (i.hasNext()){
- TestObject TO = (TestObject) i.next();
- point(TO.x, TO.y);
- }
- }
- class TestObject {
- int x, y;
- TestObject(){
- }
- }
1