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.

  1. Romans
  2. Anglo-Saxons
  3. Jutes
  4. Vikings
  5. 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.

  1. public class EnglandInvaders {
  2. public static void main(String[] args) {
  3. //Declares a string array
  4. String[] anArray;
  5. //Creates space in memory for 5 strings
  6. anArray = new String[5];
  7. //Initialize elements
  8. anArray[0] = "The first invaders were the Romans.";
  9. anArray[1] = "The second invaders were the Anglo-Saxons.";
  10. anArray[2] = "The third invaders were the Jutes.";
  11. anArray[3] = "The fourth invaders were the Vikings.";
  12. anArray[4] = "The fifth invaders were the Normans.";
  13. System.out.println("This is the order of England's invaders.");
  14. System.out.println();
  15. System.out.println(anArray[0]);
  16. System.out.println();
  17. System.out.println(anArray[1]);
  18. System.out.println();
  19. System.out.println(anArray[2]);
  20. System.out.println();
  21. System.out.println(anArray[3]);
  22. System.out.println();
  23. System.out.println(anArray[4]);
  24. }
  25. }

Java code

Use Save As to save the code to your folder as: Then compile the code in the Command prompt window. Use javac 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.

< Tribes (Int) If Else >