What i actually dont get is how can i print the contents of my list and what does each element represent as it is an Object of another class ???
The toString method provided by the ArrayList class is returning this : [javaapplication6.NewClass@7d772e]
Raymond Tong wrote:You should iterate your List and print its item.
By the way, your printObj method not actually print object, it returns a string which should be renamed.
About iterating :
I used this method to my class
But when i call this method from main it throws this exception
Exception in thread "main" java.util.ConcurrentModificationException
Java Result: 1
I also changed the code above adding Iterating
Using a for loop is similar to using an iterator but simpler for you.
It works ,it prints 0 as it should have done.Now i ll try to pass this idea to a bigger application i hope i wont find any problems there yet i would like you to explain me if you dont mind what's going on with the part :
Raymond Tong wrote:
MyClass clazz = (MyClass) myList.get(i);
this is what i dont get how its working...
So, before that, List does not specific Type it can hold.
You can add almost anything inside.
So when you try to get its content by get(n), it would return Object. (any class is implicitly extends Object)
Because you want to work on MyClass, you do type casting by.
If you want to get rid of type casting, you should use Generics, like
Now, because if has specified to save MyClass, when you get its content, it would return MyClass.
I would suggest you to read some tutorial to understand more on fundamental concept of Java
Or get a book about Java (e.g. Head First Java)