• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays - Tiger

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Can you all help me in my preparation .

Arrays
------

1] public static String toString(Object[] o)

this method returns all the elements in String format . enclosed by [] & has , between elements .

2] public static void sort(Object[] o)

this method sort the array .

int[] i = {6,3,7,4};
System.out.println(Arrays.toString(i));
i = Arrays.sort(i);
System.out.println(Arrays.toString(i));

output :
[6, 3, 7, 4]
[3, 4, 6, 7]


Is this right ? Can you add something more into this ...

My doubt is :
public static void sort(Object[] o)

it will sort object array based on natural ordering . what is this natural ordering ? can any body explain me with example .

thank you very much .
[ January 31, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API:

"The Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method."
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

So as you see in the above post, the ordering method is defined in the method compareTo(Object o) in the Comparable interface. Any class that implements this interface will define its own compareTo method. "Natural order" is a bit confusing, I'm not sure why it's "natural" as there might be different ways of ordering things according to the type of application. Only the primitive wrappers would have just one way of ordering.

For example, supposing you wanted to order colours, it could be by spectrum or hue or even by name. I once implemented Comparable on an IPAddress class, and compareTo then ordered the IP addresses by Class A, B, C, D but could just as well have ordered numerically, or by the country of origin. The point is that whatever result compareTo gives is then termed the natural order.

Hope this helps,

Alastair
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic