wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Append objects to List and show them. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Append objects to List and show them." Watch "Append objects to List and show them." New topic
Author

Append objects to List and show them.

Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Hi everybody,

i'm new to java and currently doing some practice.....
The below code consists of a MainProgram, an class Employee which return an object of Employee, created a list, and nodes.
My problem is that i am not sure if the object are being stored into the list. I've used the method append() to add to list and
the method listAll() to show the object stored into the list.

I want to print them on the screen when user chooses case 2.

I would appreciate for suggestion.. or where is my mistake because program is compiling ok but cannot output on the screen my list!!

Thanks

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
That's a lot of (unformatted, not code-tagged) code to test whether you can add items to a list and then print them out. For your own sanity's sake and to make helping you easier, you should break your projects into smaller bits, each with specific functional goals that you can test and then add to a larger project later when you've verified they do what they're supposed to do.

Edit: Helpers added code tags. Thanks.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

I added 'code tags' to your post. Notice how the formatting is preserved, making it much easier to read. Next time, after you paste in your code, highlight it and click the 'code' button above, just like you wanted to make it italic or bold.

I don't have time to look at your code in detail, but I can say that System.out.println() can be your best friend when trying to figure out what your code is doing. For example, you could print the size of your collection after each append() call to make sure it is growing appropriately.

If i have more time later (and nobody beats me to it), I'll try to take a look.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
myList is declared in main (line 17) and again in createEmployees (line 95). If you reference myList in createEmployees (such as append on line 101), it will be local to createEmployees and gone when you exit the method.



James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

In your main method, you declare and initialise myList but don't populate it anywhere.
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Sorry guys for the lot of unformatted code but this was my first time posting in here...
So where should I reference my list?

And does this mean that I should write these codes in case 2:
System.outprintln.out(myList.listAll());
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
And another thing guys does for e.g.

myList.add()
myList.append() means the same thing?
I'm using myList.append(e); in my case where Employee = e... (object)
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

At line 17, I could not stop noticing but, the following statement exists:

. List is an interface. And we cannot create an object out of it. How did the compile?. Am I missing anything here?

And by the API specs, add method appends to the list. So, they might mean the same thing.

Thanks,
Pavan.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Pavan Kumar Dittakavi wrote:At line 17, I could not stop noticing but, the following statement exists:

. List is an interface. And we cannot create an object out of it. How did the compile?. Am I missing anything here?

If you look at line 173 and later, you see that Ronald has defined his own class called List. So it's not java.util.List being used here, but Ronald's own class List.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ronald Spina wrote:And another thing guys does for e.g.

myList.add()
myList.append() means the same thing?
I'm using myList.append(e); in my case where Employee = e... (object)

Your class List doesn't have an add() method, so no, add() and append() don't mean the same thing.

People are getting confused because you've defined your own class List, you're not using the standard List interface from the package java.util. Usually it is not a good idea to name classes with the same name as some standard interface or class, because it creates confusion.
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Thanks for the reply Jesper..I guess I missed that. But one more question. In the first class, he imported the util package entirely, and then he is creating the List object. Why isn't the compiler generating an error? Why is it mapping to the one he defined and not the one in the util package?

Thanks,
Pavan.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Why have you got such a large main method? The ideal length for a main method is one statement? Why have you got so many static members in that first class?
The javac tool starts looking for classes in the current folder; if a class is found matching the name in the class, it presumes that is the class wanted. To demonstrate that phenomenon, try creating a class called String and see how many errors you can count!
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Thanks guys for so many replies.....
So first thing i should do is to change to name of the class List right.. maybe EmployeeList should be fine.

Then I need to System.out.print(myList.listAll()); to see if object are being stored.

I''ll try when I go home and hopefully will work.
Any further suggestion please forward.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Append objects to List and show them.
 
Similar Threads
istream...
For or While Statement?
Node Search
Append writeObject()
need linked list help :-)