aspose file tools*
The moose likes Beginning Java and the fly likes Something that i dont understand : Passing Objects as elements at the end of a list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Something that i dont understand : Passing Objects as elements at the end of a list" Watch "Something that i dont understand : Passing Objects as elements at the end of a list" New topic
Author

Something that i dont understand : Passing Objects as elements at the end of a list

Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
I have the following classes



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
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

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.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
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
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at javaapplication6.NewClass1.PrintList(NewClass1.java:34)
at javaapplication6.Main.main(Main.java:22)
Java Result: 1

I also changed the code above adding Iterating

Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Here is a code sample which loop through the list.
Using a for loop is similar to using an iterator but simpler for you.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
You are the MAN!!!
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);
[/code]


this is what i dont get how its working...
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Since Java 1.5, Generics is introduced.
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
Java Tutorial
Or get a book about Java (e.g. Head First Java)
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Thnx a lot Raymond I appreciate your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Something that i dont understand : Passing Objects as elements at the end of a list
 
Similar Threads
DanChisholm's assertion question
abstract class instantiation
notifyAll()
Assigning weaker privileges
Casting between classes.