//ArrayStuff.java import javax.swing.*; import java.awt.*; public class ArrayStuff extends JApplet { // public void init () { public static void main(String[] argv) { long sum=0; JTextArea textarea = new JTextArea(10,60); textarea.setLineWrap( true ); textarea.setFont( new Font("Courier",Font.BOLD,18) ); JScrollPane scrollpane = new JScrollPane(textarea); //an array is used to conveniently store lots of data. //this data can be accessed many times and places in the program. //Array contains many "elements" or "components". //array has a name, a size or length, and an element type. // declare and construct an array of ints. //each element is an int variable. int[] arr = new int[5]; //array elements automatically initialized to zero. //indexes start at 0, go to size-1, here 4 //there are 5 elements in this array. 5 int variables. arr[2] = 345; //access element indexed by 2 //it's an int varible, so can do int things with it. // arr[5] = 34543; //runtime error. index out of bounds. //the index can be a variable (and usually is) int j=3; //example arr[j] = 12345; JOptionPane.showMessageDialog(null,"element indexed by "+j+ " is "+arr[j]); //array size determined at runtime: so can be a variable String input; input = JOptionPane.showInputDialog("What size array you want?"); int size = Integer.parseInt(input); int[] A = new int[size]; //lousy name but is generic //loop over (indexes of) elements of the array. for loop typically used: for (int i=0; i max) max = A[i]; if (A[i] < min) min = A[i]; } double average; average = (double)sum / size; textarea.append("\nSum= "+sum+ " Average: "+average+ " Min value: "+min+ " Max value: "+max); JOptionPane.showMessageDialog(null, scrollpane, "Cool array stuff", JOptionPane.PLAIN_MESSAGE); } } /* show: array out of bounds: constant and variable i<=A.length runtime error int i=1 bug, missing one piece of data */