RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect13c
arrays; main
cont.

Arrays vs Lists

In practice, lists prove much more flexible than arrays: it's much more common to have data of the form “0 or more data” than “exactly 100 data”.2

actionListsarrays
The type java.util.LinkedList<Double> double[]
constructor new java.util.LinkedList<Double>() new double[50];
add-at-end myList.add( 23.4 );
access ith element myList.get(i) myArray[i]
change ith element myList.set(i,23.4) myArray[i] = 23.4
increment ith element
(useful only when the collection holds numbers)
myList.set(i, myList.get(i)+1 ) myArray[i] = myArray[i] + 1 or
myArray[i] += 1 or
++myArray[i]
determine the size/length myList.size() myArray.length
convert to String myList.toString() java.util.Arrays.toString(myArray)
Can hold primitive types? no yes

main

BlueJ has a rich interface for interacting with Java programs: you can call any method of any class. Most IDEs have a much more limited interface: They let you choose a certain class, but then you can only call one specific (static) method of that class:


/**
 * Write a description of class Acronym here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Acronym {

  /** A test driver which makes an acronym from its input,
   * printing the results to SYstem.out.
   * @param args The words of input.  Each String must be non-empty (and non-null).
   */
  public static void main( String[] args ) {
    System.out.println( "A " + args.length + "-letter acronym for "
                      + Acronym.wordsToString( args, " ", "\"", "\"" )
                      + " is "
                      + "\"" + Acronym.acronym(args) + "\""
                      + "." );
  }

  /* Return a single string, concatenating each word in an array of Strings.
   * @param words the individual Strings.
   * @param between What to put in between each word; usually " " or ", ".
   * @param before What to start the answer with; often "".
   * @param after What to finish the answer with; often "".
   */ 
  public static String wordsToString( String[] words, String between, String before, String after ) {
    String innards = "";
    if (words.length > 0) {
      innards += words[0];
      }
    for ( int i=1;  i < words.length;  ++i ) {
      innards += between + words[i];
      }
    return before + innards + after;
    }
    
  /** Return an acronym for an array-of-words, each letter capitalized.
   * and separated by a ".".
   * @param words An array of single words.  Each String must be non-empty.
   * @return an acronym for an array-of-words, each letter capitalized.
   */
  public static String acronym( String[] words ) {
    String asf = "";   // "acronym so far"
    for ( int i=0;  i < words.length;  ++i ) {
      asf += words[i].toUpperCase().charAt(0) + ".";
      }
    return asf;
    }
      
      
  }
There is nothing special about the method-name “main” except that it's been chosen as the de facto starting method by most IDEs. The input argument -- an array of Strings -- is a convention from the UNIX operating system. This input is the primary way your java program gets initial information from the operating system, when your java program is launched: a fixed number of words. For example, from a UNIX shell:
java Acronym hello, world

Review and different perspective

Arrays are like lists, but:

Why a new data structure, if a List can already do everything an array can, and yet the array is limited to a fixed size?

If a list is a line-up bag of suspects (er, objects), then an array is a bus of objects.

When creating an array, initialize all the fields immediately. If you can't, that's a good indication that you want to use a List instead.

(Note that when you add into the middle of a list, it changes what index each later object has. With an array, you can't add into the middle, so that isn't an issue. You can only overwrite the existing contents of a particular bus-seat-#.)

Processing arrays: for-loops are very natural. Also, for-each loops still work, and are perfect when you don't need the index-number to process the element.
double[] data;
data = new double[500];
java.util.Random r = new java.util.Random();

// Initialize array contents with random numbers in [0,1).
for (int i = 0;  i < double.length;  ++i ) {
  data[i] = r.nextDouble();
  }


// Find the average:
double sumSoFar = 0.0;

for ( double d : data ) {
  sumSoFar += d;
  }

double average = sumSoFar / data.length; 
// N.B. if data.length is 0, then floating-division gives Double.NaN.

1 Usually, in Java, you can extend classes to give them additional behavior. However, in Java, you can't extend array classes, so this class was kludged on.      

2 And when using a list, there are two common choices: LinkedList<Type> and ArrayList<Type>. Nearly always, ArrayList<Type> is the better choice. Despite its name, ArrayList is not an array; it's a list.      

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Apr.14 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme