|
Step 1: double[] a; // The type "double[]" is the // "array object reference" type // The variable a contains an address of // an array of doubles |
|
(And we will have learned how to define/access arrays of String)
Step 1: String[] a; // The type "String[]" is the // "array object reference" type // The variable a contains an address of // an array of Strings |
public class String01 { public static void main(String[] args) { String x; x = "Hello World"; // Assign to a String typed variable System.out.println(x); // Print a String typed variable } } |
Now apply the fact: Each array element a[i] is a String typed variable !!!
|
public class String1 { public static void main(String[] args) { /* ------------------------------------- Create a String array of 5 elements ------------------------------------- */ String[] a; a = new String[5]; /* ------------------------------------- Assign a string to each array element ------------------------------------- */ a[0] = "Hello"; a[1] = "World"; a[2] = "How"; a[3] = "are"; a[4] = "you"; /* --------------------------------------------- Print the array elements (with a for loop) --------------------------------------------- */ int i; for ( i = 0; i < a.length; i++ ) { System.out.print( a[i] + " "); } System.out.println(); } } |
How to run the program:
|
Output of this program:
Hello World How are you |
But is it not crucial that you know the internal representation
|
public class String1 { public static void main(String[] args) { /* ------------------------------------- Create a String array of 5 elements ------------------------------------- */ String[] a; a = new String[5]; /* ------------------------------------- Assign a string to each array element ------------------------------------- */ a[0] = "Hello"; a[1] = "World"; a[2] = "How"; a[3] = "are"; a[4] = "you"; /* --------------------------------------------- Print the array elements (with a for loop) --------------------------------------------- */ int i; for ( i = 0; i < a.length; i++ ) { System.out.print( a[i] + " "); } System.out.println(); } } |
Note:
|
|
Example:
|
|
|
|
a = String array of 100 elements; int numWords = 0; Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { a[numWords] = read next word from the Scanner object; numWords++; // Use the NEXT element in array to store // the next word in file ! } /* -------------------------------------------------------------- We can process the words in the array here if we so desire... -------------------------------------------------------------- */ ... (further array processing) ... |
import java.io.*; import java.util.Scanner; public class ReadText1 { public static void main(String[] args) throws IOException { String[] a = new String[100]; // Create String array of 100 elem's int numWords; // Count # words in input File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file numWords = 0; while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } System.out.println("Printing the array..."); for ( int i = 0; i < numWords; i++ ) System.out.println( a[i] ); } } |
How to run the program:
|
Advanced Java/C programmers write this loop:
while ( in.hasNext() ) { a[numWords] = in.next(); // Read next string (word) numWords++; // Count # words AND use next // array element for next word } |
as follows:
while ( in.hasNext() ) { a[numWords++] = in.next(); } |
(because numWords++ evaluates to the old value of numWords).