943,910 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1546
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 11th, 2009
0

Printing an array alphabetically

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007
Apr 11th, 2009
0

Re: Printing an array alphabetically

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Apr 11th, 2009
0

Re: Printing an array alphabetically

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.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Apr 11th, 2009
0

Re: Printing an array alphabetically

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.
Thanks for your quick response.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007
Apr 11th, 2009
0

Re: Printing an array alphabetically

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.
lol ok ok thats why i obviously need help....

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007
Apr 11th, 2009
0

Re: Printing an array alphabetically

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.

java Syntax (Toggle Plain Text)
  1. import java.util.TreeSet;
  2. import java.util.SortedSet;
  3.  
  4. public class TreeSet_Test{
  5.  
  6. public static void main(String... args){
  7.  
  8. String values[] = {"T", "V", "E", "D", "A", "B"};
  9.  
  10.  
  11. TreeSet<String> tree = new TreeSet<String>();
  12.  
  13. for(String element : values)
  14. tree.add(element);
  15.  
  16. SortedSet<String> result = tree.tailSet("A");
  17.  
  18. for(String element : result)
  19. System.out.print(element + " ");
  20. }
  21.  
  22. }
Last edited by Alex Edwards; Apr 11th, 2009 at 1:16 pm.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Apr 11th, 2009
0

Re: Printing an array alphabetically

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 appreciate your help...

but did i mention I was pretty new to java lol
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007
Apr 11th, 2009
0

Re: Printing an array alphabetically

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
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Apr 11th, 2009
0

Re: Printing an array alphabetically

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007
Apr 11th, 2009
0

Re: Printing an array alphabetically

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
Thanks... i'll have a look now... you must have read my mind... I was posting my message before I saw yours
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek-girl is offline Offline
6 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: storing a polynomial
Next Thread in Java Forum Timeline: HTML link in Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC