Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Arrays -- Examples

[an error occurred while processing this directive]

This applet shows a number of methods that use arrays. The source code for the methods is also given below.

This applet will not display correctly unless your browser supports Java 1.2.

Sort

There are many kinds of sort programs. These are examples showing some common ways of writing two simple sorts: selection sort, and bubble sort. NOTE: You should rarely, if ever, write your own sort. Good sorts are available in the Java library in the java.util.Arrays.sort(...) and java.util.Collections.sort(...) methods.

Here are some questions that you should be able to answer about the sort algorithms.

  1. Does this sort from low to high, or high to low?
  2. How can you change it to sort in the other direction.
  3. How can you change this to sort doubles?
  4. How can you change this to sort Strings?
  5. How many comparisons and exchanges are made?
  6. How much work does it do if the array is already sorted?
  7. [an error occurred while processing this directive] How much work does it do if the array is already sorted backwards?
  8. After each pass, where are the sorted numbers?

Median

This method computes the median (middle) value of an array. The array must be sorted before calling this method. It returns the middle element, or the average of the two middle elements if there are an even number of elements. Actually, the entire array doesn't need to be sorted, only up to and including the middle element(s).

//================================================== median
//   Precondition: Array must be sorted
public static double median(double[] m) {
    int middle = m.length/2;  // subscript of middle element
    if (m.length%2 == 1) {
        // Odd number of elements -- return the middle one.
        return m[middle];
    } else {
       // Even number -- return average of middle two
       // Must cast the numbers to double before dividing.
       return (m[middle-1] + m[middle]) / 2.0;
    }
}//end method median

Question: Does it matter if the array is sorted in ascending or descending order?

Fill an array with random values

Puts a random integer into each element in the array. Math.random() returns a double in the range 0.0-1.0, therefore we have to change the range, and cast to int.
//=============================================== fillRandom
public static void fillRandom(int[] a, int min, int max) {
    int range = max-min;
  
    for (int i=0; i < a.length; i++) {
        a[i] = min + (int)(Math.random()*range);
    }
}//endmethod fillRandom

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.