Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
Sorting an array

 
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum
Author Message
dude
Power User
Power User


Joined: 10 Mar 2004
Posts: 376
Location: Savar, Dhaka

Post Post subject: Sorting an array Reply with quote

Hi,

Suppose you have an array of words. What is the easiest way to sort and print the array alphabetically?

Thanks.
Wed Mar 10, 04 2:56 am
Back to top
dude View user's profile Send private message Visit poster's website
Belal
User
User


Joined: 08 Mar 2004
Posts: 84
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Hi
if u like to sort ur word array without knowing anything about sorting algorithm, it will be better for u to use the 'Arrays' class under 'java.util.Arrays'. Just import the package at the beginning of ur program and in ur code send ur array in a statement like

Arrays.sort(ur_word_array_name);

and then print the array, to get the sorted words. Remember u may have to include ArrayIndexOutOfBoundsException or NullPointerException.

Thanks
looking forward for ur reply
belal
Wed Mar 10, 04 4:34 am
Back to top
Belal View user's profile Send private message Yahoo Messenger MSN Messenger
dude
Power User
Power User


Joined: 10 Mar 2004
Posts: 376
Location: Savar, Dhaka

Post Post subject: Reply with quote

No, i need to sort the array using some sort algorithm. So just importing the array utility won't work. What kind of sorting algorith can i use here?
Thu Mar 11, 04 6:30 am
Back to top
dude View user's profile Send private message Visit poster's website
Belal
User
User


Joined: 08 Mar 2004
Posts: 84
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

Hi
here is one simple sort algorithm(bubble sort), try with this and inform me

//to sort ur array with bubble sort
for(int i=0;i<T;i++) //here T is the size ur words' array
for(int j=0;j<T-1;j++)
{
if(a[j].compareTo(a[j+1])>0) //a is the name of ur array
{
temp=a[j]; //temp is a String type data
a[j]=a[j+1];
a[j+1]=temp;
}
}
//to print the array
for(int i=0;i<T;i++)
System.out.println(a[i]);


Thanks
belal
Thu Mar 11, 04 8:51 am
Back to top
Belal View user's profile Send private message Yahoo Messenger MSN Messenger
dude
Power User
Power User


Joined: 10 Mar 2004
Posts: 376
Location: Savar, Dhaka

Post Post subject: Reply with quote

That was very helpful. Thank you Mr. Belal. It worked! but tell me please, what does the compare.To actually do here? I suppose it's a builtin function. Also can you describe easily, what's a method, constructor?
Sat Mar 13, 04 12:15 pm
Back to top
dude View user's profile Send private message Visit poster's website
Belal
User
User


Joined: 08 Mar 2004
Posts: 84
Location: Dhaka, Bangladesh

Post Post subject: Reply with quote

dude it's good to know that my answer helped u.

here is a small suggestion for u. when u need to know abt a builtin method
u can find with a java api documentaion. hope u have that or u will collect
that for ur future guideline.

in my code the method compareTo(String s), is a method that help us to compare two string. it compares the main string(the string variable after which we r using dot(.) and this method e.g 'a[j]' in my example) with the parameter string that is passed into the compareTo() method's parenthesis. and returns negative value if main string is smaller i.e. come before the parameter string and else postive value.

when we say 'method' it means to gather similar functions or a specific funtion statements that we write using any programming language. we can identify a method seeing a pair of parenthesis '()' after a 'methodName' e.g. compareTo().......remember to use a method we have to know if there requires any parameter value or we can simply call it.
in java we define a method like-----
<format>
returnType methodName(parameterLists or empty if no parameter)
{
//required statements
}
<end of format>
<example>
int square(int a) //here we means the method will return an integer value
// and required an integer value to call it
{
return a*a;
}

or,

void printValue(int a) //here we means that the method will not return any
//value by using the keyword 'void' and it will just
//print the value that will be used to call the method
{
System.out.println(a+"");
}

if ur method name and class file name is same, then we say the method is a constructor. And the purpose of this method is to initial the private variable values or define the initial state of the class that will b obserbed when an object is instantied from the class.

i think it getting long.........next time, have good time, waiting for ur opinion
belal
Tue Mar 16, 04 8:35 am
Back to top
Belal View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group