Espresso: A Concentrated Introduction to Java


Short Reference on Arrays

Array Declaration
Type[] name
E.g., int[] grades
E.g., String[] names
Array Construction
new Type[size]
E.g., grades = new int[5];
E.g., names = new String[num_students + num_faculty];
Array Initialization During Construction
new Type[] { v0, v1, ..., vn };
E.g., new int[] { 5, 3, 1 }
E.g., new String[] { "Hello", "Goodbye" }
E.g., new Fraction[] { new Fraction("1/3"), new Fraction(a,b) };
Array Length
name.length
E.g., pen.println("I have recorded " + grades.length + " grades for you.");
Getting a value
name[index]
E.g., pen.println(names[i]);
Setting a value
name[index] = newvalue
E.g.,. grades[i] = 100;

History

Saturday, 5 March 2005


This page was generated by Siteweaver on Thu Mar 30 15:24:44 2006.
The source to the page was last modified on Sun Mar 6 00:42:17 2005.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Reference/arrays.html.

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu