Invaders (Array)
In addition to all the migration that was experienced in what is now England. The Brittons (the collective name given to the tribes of the southern British Isles) experienced five major invasions from continental Europe.
Here is a list of the five invaders.
- Romans
- Anglo-Saxons
- Jutes
- Vikings
- Normans
When you have large numbers of variables you can use an array to reserve space in the memory of the computer.
This code illustrates the use of a string array.
- public class EnglandInvaders {
- public static void main(String[] args) {
- //Declares a string array
- String[] anArray;
- //Creates space in memory for 5 strings
- anArray = new String[5];
- //Initialize elements
- anArray[0] = "The first invaders were the Romans.";
- anArray[1] = "The second invaders were the Anglo-Saxons.";
- anArray[2] = "The third invaders were the Jutes.";
- anArray[3] = "The fourth invaders were the Vikings.";
- anArray[4] = "The fifth invaders were the Normans.";
- System.out.println("This is the order of England's invaders.");
- System.out.println();
- System.out.println(anArray[0]);
- System.out.println();
- System.out.println(anArray[1]);
- System.out.println();
- System.out.println(anArray[2]);
- System.out.println();
- System.out.println(anArray[3]);
- System.out.println();
- System.out.println(anArray[4]);
- }
- }
Use Save As to save the code to your folder as: EnglandInvaders.java. Then compile the code in the Command prompt window. Use javac EnglandInvaders.java to create the .class file. Execute the program. Use java EnglandInvaders to run the program.
Your output should look like this:
c:\javamike>java EnglandInvaders
This is the order of England's invaders.
The first invaders were the Romans.
The second invaders were the Anglo-Saxons.
The third invaders were the Jutes.
The fourth invaders were the Vikings.
The fifth invaders were the Normans.