| | |
Printing an array alphabetically
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
Hi,
I'm pretty new to java and I'm not asking for anyone to do any coding for me. I just need pointing in the right direction.
How do I go about printing out the objects of an array sorted alphabetically by name without altering the original array. I know I can't use the .sort method to achieve this and I don't want to make a new temp array to do this either.
I know I have to use the compareTo method somewhere....
I'm just confused
Any help appreciated
Thanks
I'm pretty new to java and I'm not asking for anyone to do any coding for me. I just need pointing in the right direction.
How do I go about printing out the objects of an array sorted alphabetically by name without altering the original array. I know I can't use the .sort method to achieve this and I don't want to make a new temp array to do this either.
I know I have to use the compareTo method somewhere....
I'm just confused
Any help appreciated
Thanks
More details are needed.
For example, what type of goal are you trying to achieve? Finding a low-running time way of printing your array or just getting the job done?
For example, what type of goal are you trying to achieve? Finding a low-running time way of printing your array or just getting the job done?
The Iterative/Recursive approach T, V, E, D, A, B T is found V is found, V should be printed after T E is found, E should be printed before T D is found, D should be printed before T, before E A is found, A should be printed before T, before E, before D B is found, B should be printed before T, before E, before D but after A Once all of the indexes in the array are reached, recursively print the results correctly.
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
More details are needed.
For example, what type of goal are you trying to achieve? Finding a low-running time way of printing your array or just getting the job done?
The Iterative/Recursive approach T, V, E, D, A, B T is found V is found, V should be printed after T E is found, E should be printed before T D is found, D should be printed before T, before E A is found, A should be printed before T, before E, before D B is found, B should be printed before T, before E, before D but after A Once all of the indexes in the array are reached, recursively print the results correctly.
I am looking for a way of using as little code as possible and using any of the java library classes available for the job.
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
I have no idea why you want to do this! What's wrong with sorting a copy? Remember that the copy array, like the original, is simply an array of references (like pointers) to the Strings; you won't copy the actual Strings unless you go out of your way so to do.
so for example... I have an array of contact items (each consisting of first names, surnames and numbers) and I create a temp array... then how do I sort the temp array in order of their surnames so they can be printed
thanks
Since any Java-API based class is valid for the assignment....
I suggest inserting the values into a B-Tree (Binary Tree) then printing the objects in the tree via In-order traversal.
I suggest inserting the values into a B-Tree (Binary Tree) then printing the objects in the tree via In-order traversal.
java Syntax (Toggle Plain Text)
import java.util.TreeSet; import java.util.SortedSet; public class TreeSet_Test{ public static void main(String... args){ String values[] = {"T", "V", "E", "D", "A", "B"}; TreeSet<String> tree = new TreeSet<String>(); for(String element : values) tree.add(element); SortedSet<String> result = tree.tailSet("A"); for(String element : result) System.out.print(element + " "); } }
Last edited by Alex Edwards; Apr 11th, 2009 at 1:16 pm.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
Create a copy of your array and sort it using the built-in sort method, but with a custom Comparator...
define your own comparator (see the API) for your Contact class, which calculates its results by simply comparing the surnames.
See http://www.javaworld.com/javaworld/j...rt.html?page=2 for more details
define your own comparator (see the API) for your Contact class, which calculates its results by simply comparing the surnames.
See http://www.javaworld.com/javaworld/j...rt.html?page=2 for more details
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
ok... back to basics
I would just like to sort my arraylist of contacts alphabetically by name.
I have different types of contacts... with contact being the superclass (and is also an abstract class). I have also tried to create a comparable interface... but when I come to implement it I get an error... something to do with my abstract class.
I'm getting really confused now... I just thought it would be a simple process...
I would just like to sort my arraylist of contacts alphabetically by name.
I have different types of contacts... with contact being the superclass (and is also an abstract class). I have also tried to create a comparable interface... but when I come to implement it I get an error... something to do with my abstract class.
I'm getting really confused now... I just thought it would be a simple process...
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Create a copy of your array and sort it using the built-in sort method, but with a custom Comparator...
define your own comparator (see the API) for your Contact class, which calculates its results by simply comparing the surnames.
See http://www.javaworld.com/javaworld/j...rt.html?page=2 for more details
![]() |
Similar Threads
- C# Help! Arrays, ListBox's and Buttons (C#)
- Copying words into an array of char ?!? (C)
- Please I need help formatting an output file and sorting the names! (C++)
Other Threads in the Java Forum
- Previous Thread: storing a polynomial
- Next Thread: HTML link in Java
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





