Intro. to Programming Workshop - 3

Welcome back to the Intro. to Programming Workshops! This week's workshop will focus on arrays!
The first version of the PokeVentures game source code is now available! Each week, there will be upgrades to the base source code which runs on the libgdx framework. Please feel free to modify the code as you please!

Resources

Setting up Gradle for PokeVentures

The PokeVentures game requires quite a large number of external libraries to be compiled before it can run, so a build tool known as Gradle will help us do the compiling instead of the usual javac.

Instructions

  1. Download the complete distribution of Gradle from: http://gradle.org/
  2. Extract the downloaded .zip file to your desired destination.
  3. Add the <Gradle-install-folder>/bin/ folder to your System Environment Variables Path. (Done the same way as setting up Java on the command-line - Intro. to Programming Workshop 1)

Summary

Arrays

  • Syntax:
int[] meow;        // type[] name;  
meow = new int[6]; // name = new type[size];  
  • Pros: Excellent for storing a sequence of items
  • Cons: Fixed size upon creation
  • Alternative Syntax:
int[] shortMeow = new int[6]; // Option 1  
int[] specificMeow = {0, 0, 0, 0, 0, 0}; // Option 2  
  • 2D-array Syntax:
int[][] meowception;         // type[][] name;  
meowception = new int[2][3]; // name = new type[size1][size2]  
  • Alternative Syntax for 2D-arrays:
int[][] shortMeowception = new int[2][3];  
int[][] specificMeowception =  
{
  {0, 0, 0},
  {0, 0, 0}
};
int[][] sameAsAboveMeow = {{0, 0, 0}, {0, 0, 0}};  

Exceptions

  • ArrayOutOfBoundsException - The compiler will throw this Exception whenever you try to access an index of an array that doesn't exist.
String[] words = {"Nup", "Yep"}; // Array has size 2  
words[0] = "Poochyena";          // OK  
words[2] = "Skitty";             // ArrayIndexOutOfBoundsException: 2  
  • NullPointerException - An extremely common Exception and simply thrown whenever you try to access a variable with no initial value.
String[] words;         // No initial value yet  
words[0] = "Poochyena"; // Causes a NullPointerException