Source of ArrayBasics.java


  1: import java.util.Scanner;

  3: /**
  4:  * Some simple array manipulations.
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  */
  8: public class ArrayBasics {

 10:     /**
 11:      * Run this program.
 12:      *
 13:      * @param args command lines arguments (ignored)
 14:      */
 15:     public static void main(String[] args) {
 16:         // introduce self
 17:         System.out.println("\n"
 18:             + "Just doing some array things."
 19:             + "Look at the code and compare it to the output.\n");

 21:         // an array type is just another data type
 22:         int anIntVar;                   // type int
 23:         int[] aBunchOfIntVars;          // type int[]

 25:         // you can make an array out of /any/ data type
 26:         double[] aBunchOfDoubleVars;    // doubles
 27:         String[] aBunchOfStringVars;    // Strings
 28:         Scanner[] aBunchOfScannerVars;  // Scanners
 29:         int[][] aBunchOfIntArrayVars;   // int[]s (!)

 31:         // like other variables, array vars need to be given values
 32:         // like Scanners, use the word "new" to make the value
 33:         Scanner kbd = new Scanner(System.in);
 34:         // unlike Scanners, it's []s after the data type, not ()s
 35:         double[] someNumbers = new double[20];
 36:         String[] someWords = new String[1000];

 38:         // size of the array can be a variable
 39:         System.out.print("How many lines will you be entering? ");
 40:         int numLines = kbd.nextInt();   kbd.nextLine();
 41:         String[] theLines = new String[numLines];

 43:         // Array usually used as a group
 44:         // (doing the same thing to each element)
 45:         for (int i = 0; i < someNumbers.length; i++) {
 46:             // pick out array elements using []
 47:             someNumbers[i] = Math.sqrt(i);
 48:             // number inside the brackets is called an index
 49:         }
 50:         // but you can use array elements as individual variables
 51:         System.out.println("someNumbers[10] == " + someNumbers[10]);

 53:         // read some lines
 54:         System.out.println("\n"
 55:             + "Let's enter those lines, now:");
 56:         for (int i = 0; i < numLines; i++) {
 57:             theLines[i] = kbd.nextLine();   // no need to tidy up
 58:         }

 60:         // print the lines (reversed)
 61:         System.out.println("\n"
 62:             + "Here are the lines you entered, in reverse order:");
 63:         for (int i = numLines - 1; i >= 0; i--) {
 64:             System.out.println("\t\"" + theLines[i] + "\"");
 65:         }
 66:         System.out.print("\n---Press Enter to continue---");
 67:         kbd.nextLine();

 69:         // print the square roots
 70:         System.out.println("\n"
 71:             + "And here are the square roots I calculated earlier:");
 72:         for (int i = 0; i < someNumbers.length; i++) {
 73:             System.out.println("\tsqrt(" + i + ") is " + someNumbers[i]);
 74:         }
 75:         System.out.println();
 76:     }

 78: }