/* This is a program that demonstrates arrays: declaration, reading and writing. Laura Toma csci 101 */ public class ReadArray { public static void main (String args[]) { //setting up the variable that reads from user ReadStream r = new ReadStream(); int nbElements; System.out.println("Please enter the number of elements: "); nbElements = r.readInt(); r.readLine(); //declare an array of that many elements int a[] = new int [nbElements]; //Note: after the array is declared, it knows its size; we can get //it with a.length //read the elements in the array from the user; to do this we //set up a counter, and we make it go through all positions of //the elements in the array int i = 0; while (i < a.length) { System.out.println("Enter element " + i + ": "); a[i] = r.readInt(); r.readLine(); i = i+1; } //the array stores the elements we entered //What can we do with the array? For example, we can print the //elements in the array; for this, again, we need a counter i = 0; while (i < a.length) { System.out.println("Element " + i + "=" + a[i]); i = i+1; } } // end of main } // end of class