Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Java, sorting, ArrayList example, generics

A Java ArrayList holds an ordered sequence of items like an array, but there are differences:

1. An ArrayList has no fixed size but can be extended by adding elements later
2. An ArrayList can only hold objects (not primitives)
3. ArrayList elements are addressed by methods and not by square bracket notation.

Here's an example from yesterday - showing a typical use of an ArrayList; I was reading a file but didn't know how many lines it contained, so an ArrayList gave me the flexibility I needed.

import java.io.*;
import java.util.*;
 
public class Pesort {
 
public static void main (String [] args) throws Exception {
 
   // Following line up to Java 1.4 then gives warnings
   //ArrayList Stuff = new ArrayList();
 
   // OR following line from Java 1.5;
   // this specifies that the ArrayList is to contain Strings
 
   ArrayList <String> Stuff = new ArrayList<String>();
 
   // Above lines show use of "Generics" - compare to
   // Templates in C++
 
   BufferedReader Source = new BufferedReader(
      new FileReader("../request.txt"));
 
   System.out.println("---------- UnSorted");
 
   while (Source.ready() ) {
      String Line = Source.readLine();   
      System.out.println(Line);
      Stuff.add(Line);
      }
 
   System.out.println("---------- Alphabetic Sort");
   
   Collections.sort(Stuff);
 
   Iterator stepper = Stuff.iterator();
   while (stepper.hasNext()) {
      String current = (String)stepper.next();
      System.out.println(current);
      }
   
 
   System.out.println("---------- Sort by line length");
 
   Collections.sort(Stuff, new byLineLength());
 
   stepper = Stuff.iterator();
   while (stepper.hasNext()) {
      String current = (String)stepper.next();
      System.out.println(current);
      }
   }
 
}


Here are the results from running that program:


[trainee@snowdrop java08]$ java Pesort
---------- UnSorted
antonia Perl XML PHP Tcl/Tk MySQL
barbara Tcl/Tk ASP Ruby Java
cherry Perl Java Ruby MySQL
delia XML PHP Java ASP
florence Ruby PHP Java ASP
gloria XML Perl Tcl/Tk MySQL
zoe Ruby ASP Perl PHP
adam Tcl/Tk Perl Python MySQL
barry Python XML Java Perl PHP
charles Perl Ruby MySQL Tcl/Tk
ed Ruby Perl Java PHP
fred MySQL Perl Java XML
graham Java Perl Tcl/Tk
harry PHP Python Java
ivan Ruby Java Perl Tcl/Tk MySQL
victor Ruby Perl Tcl/Tk MySQL
william Ruby Perl PHP
xavier PHP Java Perl
yuri XML PHP Perl Tcl/Tk
zachary MySQL Java Tcl/Tk
---------- Alphabetic Sort
adam Tcl/Tk Perl Python MySQL
antonia Perl XML PHP Tcl/Tk MySQL
barbara Tcl/Tk ASP Ruby Java
barry Python XML Java Perl PHP
charles Perl Ruby MySQL Tcl/Tk
cherry Perl Java Ruby MySQL
delia XML PHP Java ASP
ed Ruby Perl Java PHP
florence Ruby PHP Java ASP
fred MySQL Perl Java XML
gloria XML Perl Tcl/Tk MySQL
graham Java Perl Tcl/Tk
harry PHP Python Java
ivan Ruby Java Perl Tcl/Tk MySQL
victor Ruby Perl Tcl/Tk MySQL
william Ruby Perl PHP
xavier PHP Java Perl
yuri XML PHP Perl Tcl/Tk
zachary MySQL Java Tcl/Tk
zoe Ruby ASP Perl PHP
---------- Sort by line length
xavier PHP Java Perl
ed Ruby Perl Java PHP
harry PHP Python Java
william Ruby Perl PHP
zoe Ruby ASP Perl PHP
delia XML PHP Java ASP
graham Java Perl Tcl/Tk
fred MySQL Perl Java XML
yuri XML PHP Perl Tcl/Tk
zachary MySQL Java Tcl/Tk
florence Ruby PHP Java ASP
cherry Perl Java Ruby MySQL
barbara Tcl/Tk ASP Ruby Java
gloria XML Perl Tcl/Tk MySQL
adam Tcl/Tk Perl Python MySQL
victor Ruby Perl Tcl/Tk MySQL
barry Python XML Java Perl PHP
charles Perl Ruby MySQL Tcl/Tk
ivan Ruby Java Perl Tcl/Tk MySQL
antonia Perl XML PHP Tcl/Tk MySQL
[trainee@snowdrop java08]$


And here is the comparator class that I wrote for my non-standard sort. Using a comparator class, the programmer can provide a method which is used internally by the sort method to compare two items and return an integer - negative, nought or positive - to indicate if the first object comes first in sequence, the objects have the same tank, or it comes second.

public class byLineLength implements java.util.Comparator {
 public int compare(Object boy, Object girl) {
  int sdif = ((String)boy).length() - ((String)girl).length();
  return sdif;
 }
}

(written 2008-01-11, updated 2008-01-12)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J714 - Java - Fundamental classes
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [1910] Java - Generics - (2008-11-27)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)


Back to
Java - using super to call a method in the parent class
Previous and next
or
Horse's mouth home
Forward to
Web page (http) error status 405
Some other Articles
Ongoing Image Copyright Issues, PHP and MySQL solutions
Script to present commonly used images - PHP
Flooding by Asda-s proposed new supermarket
Web page (http) error status 405
Java, sorting, ArrayList example, generics
Java - using super to call a method in the parent class
First Class Java. First step and moving forward.
Climate change, renewable resources and paper v plastic
Java is a dynamic language .... (and comparison)
Training Season Starts again!
4255 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1502_Jav ... erics.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat