• 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

Doubt in Dans example

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys
I was going thru Dans tutorial.At chap 9 ,java.util package there is one example for collections.The code is

import java.util.*;
public class Sort{
public static void main(String argv[]){
Sort s = new Sort();
}

Sort(){
String s[] = new String[4];
s[0]="z";
s[1]="b";
s[2]="c";
s[3]="a";
Arrays.sort(s);
for(int i=0;i< s.length;i++)
System.out.println(s[i]);
}
}
In this code i dont understand the line
Arrays.sort(s);
What is this?
Thanx in adv.
Renga.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the java.util.Arrays API, you will find the sort methods there.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renga,

Arrays is a class in util. sort is a method in that class.
General format is :

sort( char Array1[])

sort( char Array1[], int From, int To)

You can use byte, short, int, long, char, object, double, float types.

Hope it helps
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx barry/nitin
But the code is failing to compile.
When i went thru the API i could not find a sort method which takes a String array as argument.Is this the reason it is not compiling,is the code wrong or there could be any other reason.
Thanx
renga.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not getting any compilation error with Java 1.5. It runs and prints a b c z (on separate lines).

What error are you getting?

By the way, the appropriate method is Arrays.sort(Object[] array). A String is an Object, right? And String implements the Comparable interface.
[ October 22, 2004: Message edited by: Barry Gaunt ]
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,
Sorry, i wasnt in yday,so couldnt reply.
This is the error i get while compiling the code.

Sort.java [14:1] cannot resolve symbol
symbol : method sort (java.lang.String[])
location: class Arrays
Arrays.sort(s);
^
1 error
Errors compiling Sort.

I dont know what is the problem.I use NetBeans IDE 3.6 to run my programs.
Could u clarify.
Thanx
Renga.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Renga,
What happens when you compile it using the Java 1.4.2 compiler from the command line?
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,
Im sorry i dont know how to compile it from command line using 1.4.2 compiler.Is it possible for u to explain it to me.
Thanx in adv
Renga
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you read this.
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx barry
i will do it and get back to u
renga
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry
Something strange.
I just made one change in my program in the same IDE and now it compiles and executes fine.From the import statement i changed from
import java.util.*; to
import java.util.Arrays;
it works fine,what could be the reason?
thanx
renga
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, no idea. Both of those should work.
 
reply
    Bookmark Topic Watch Topic
  • New Topic