Okay, someone help me here. I am trying to figure out why this code won't compile when put together with a main method that prints out names. ArrayList has a method named add that takes an int for the index and an object for the element. I created Name as a class to make a Name object to pass into the add() method, but when I try to compile, I get this error:
sorry Carol I've deleted youre codeJdJ Can anyone give me a clue as to why the method won't work with the parameters I've given? Aghhhhhhhhhh!!! [This message has been edited by Johannes de Jong (edited July 01, 2001).]
Create an ArrayList. Add the name to the end of (append the name to) the ArrayList. You shouldn't need 'int' because you shouldn't need to insert the name into the ArrayList. Let the program do the sorting after you've populated the ArrayList.
The name is a String. A String is an object, so you can use add( object )
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Mar 15, 2001
Okay, so now I've got this ArrayList which looks like this: [ name, name, name, blah, blah ] How do I access the individual elements? I tried get() and set() but no luck. If I use the name inside double quotes, I get a message saying that String isn't the right parameter. The parameters in my book are either int or Object and I can't figure this out. Any clues?
Hi Carol, Here is a link Sun Java Collection Tutorial you want to look at if you haven't already. To access the ArrayList use the ListIterator interface and its hasNext() and next() methods. The hasNext() is used to see if the end of the list has been reached and the next() returns the next element in the list. You need a while loop with the hasNext() in the conditional check. Hope this helps
[This message has been edited by Richard Boren (edited July 03, 2001).]
Joined: Mar 15, 2001
Thanks for your help. I have the list in alphabetical order now by first name only. The tutorial is very helpful, but I have a big problem with the language. I don't understand a lot of it. Feel kinda stupid, but hey, at least part of the program works! I need to whack away at getting the sort by last names now, but my ego needs a little rest. Happy fourth!
Joined: Mar 01, 2001
Hey Carol, Don't feel stupid. This is whole new way of thinking and it takes a little getting use too. "Listen to me, talking like I grasp the whole concept." Once you do find the solution for sorting by last name order the OOP idea of code reuse will be greatly re-enforced. So don't feel bad, I am sure there are several of us that are struggling with the OOP concept