File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help Please 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 "Help Please" Watch "Help Please" New topic
Author

Help Please

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I need to add a name to an array but I have to make add it in alphabetical order. I got the add part down its how to do it in aphabetical order. Any help would be apprecated
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Does it have to be an array? What about a SortedSet?


There is no emoticon for what I am feeling!
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
It has to be an array....I already have an array of names and I am suppose to be able too add names to get and when I add them I am suppose to do it alphabetically
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You could put the array in a sorted collection, add the element, and then turn the collection into an array.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Is this an assignment?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
m I reading that right...is a sortedSet an intereface...if so I can noy use it...I have to right everything...I can not use plugs...if I could believe me I wouldhave used array lists for this project
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I do have a compareTo method that I wrote that I can use but I just don't know how to go about doing so.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
When you need to add a new element to the array, create a second array, copy the contents of the original into it, and add the new element into it. Then you could use a sorting algorithm to sort the array.
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
Yes this is an assigment....a small part of it
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
You could also use <code>java.util.Arrays.sort(Object[] array)</code> to sort the array when ever you need it to be correctly ordered -- this assumes that your objects in the array are Comparable or that you use the version of the method that also takes a Comparator.

Hope this helps.


- Chris Stehno, SCPJ
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
when you say a sorting alogithm do you mean a for loop of some kind
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Actually, I guess a more efficient way to do it would be since the first array is by definition sorted, you could create the second array, and copy the elements from the first array into it as long as the elements are "less than" what you want to add. When you get to the first element in the original array that is "greater than" what you want to add, then add your element to the new array, and then copy the remainder from the original array into the new one.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
With those requirement I would do something like this:


Garrett


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
since I have a compareTo() method can't I just start with a for loop...like so

for(int i=0; i<list.length; i++)
{
CODE HERE somehow using the comapre to method I have
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Please
 
Similar Threads
Add an editable row to an non editable JTable
Help me,about part II,Thanks
Retrieval of elements in Set
Regarding sorting elements in ArrayList
How to use restoreState() method?