File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Sort this!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Sort this!!!!!" Watch "Sort this!!!!!" New topic
Author

Sort this!!!!!

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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 code JdJ
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).]
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
But after having done that.
You don't add Arraylist entries with keys.
Pass only the string as a paramater.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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?
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
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).]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
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!
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
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
 
GeeCON Prague 2014
 
subject: Sort this!!!!!