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!!
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
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
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.
Ronald Spina wrote:And another thing guys does for e.g.
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.
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?
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!
Joined: Jan 23, 2012
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.