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
posted
0
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
posted
0
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: 2341
posted
0
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
posted
0
Is this an assignment?
Victoria Preston
Ranch Hand
Joined: Feb 03, 2006
Posts: 106
posted
0
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
posted
0
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: 2341
posted
0
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
posted
0
Yes this is an assigment....a small part of it
Chris Stehno
Ranch Hand
Joined: Feb 26, 2001
Posts: 180
posted
0
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
posted
0
when you say a sorting alogithm do you mean a for loop of some kind
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
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: 1295
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.