Printing an array alphabetically

Reply

Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Printing an array alphabetically

 
0
  #1
Apr 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Printing an array alphabetically

 
0
  #2
Apr 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Printing an array alphabetically

 
0
  #3
Apr 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Re: Printing an array alphabetically

 
0
  #4
Apr 11th, 2009
Originally Posted by Alex Edwards View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Re: Printing an array alphabetically

 
0
  #5
Apr 11th, 2009
Originally Posted by JamesCherrill View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Printing an array alphabetically

 
0
  #6
Apr 11th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Re: Printing an array alphabetically

 
0
  #7
Apr 11th, 2009
Originally Posted by Alex Edwards View Post
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Printing an array alphabetically

 
0
  #8
Apr 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Re: Printing an array alphabetically

 
0
  #9
Apr 11th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: geek-girl is an unknown quantity at this point 
Solved Threads: 0
geek-girl geek-girl is offline Offline
Newbie Poster

Re: Printing an array alphabetically

 
0
  #10
Apr 11th, 2009
Originally Posted by JamesCherrill View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC