Arrays

Dr Andy Evans

[Fullscreen]

Variables

  • Remember that we make a variable, thus:
    	
    double a = 10.0;
    
    
    or,
    	
    Point p1 = new Point();
    
    
    But what if we want to read in a big datafile?

Arrays

  • Do we need a new name for each data point?
  • Ideally we'd just have a list or table of data, and get at the data with the list/table name and the location within it
  • This structure is called an array.
  • Arrays are variables with multiple examples of the same kind of object in them.
  • E.g. a 'xCoord' array variable containing 10 different x values.

Naming

  • As for all variables, we first make a label. The syntax is:
    	
    type arrayName[];          
    
    
    or,
    	
    type[] arrayName;
    
    
  • For example:
    	
    double xCoords[];
    double []xCoords;
    Point pathway[];
    
    

Assigning

  • Then we attach it to a suitable set of spaces in memory.
    	
    arrayName = new type[size];
    
    
    e.g.,
    	
    xCoords = new double[10];
    pathway = new Point[10];
    
    
  • This has now made 10 spaces in our arrays, numbered 0 to 9.
  • Arrays can be declared and assigned at the same time:
    	
    double xCoords[] = new double [10];
    
    

Using

  • We fill them by using the name and index location:
    	
    xCoords[0] = 100.0;
    pathway[9] = somePoint;
    
    
  • And likewise to get out the value:
    	
    myX = xCoords[0];
    
    
    Gives myX as 100.0.
  • You use them with objects like any normal name:
    	
    myX = pathway[9].x
    
    
  • If you try to use a space that doesn't exist, you'll break the program.

Primitive arrays

  • Arrays are just label collections.
  • Java fills primitive arrays with default values:
    	
    int		: 0
    double	: 0.0
    char	: spaces
    boolean	: false 
    
    
  • But you should do this anyhow, so you are sure what is in there.

Object arrays

  • For objects, the unassigned labels just point at null.
  • So, the first thing you need to do is attach each label to an object:
    	
    Point[] pathway = new Point[10];
    pathway[0] = new Point();
    System.out.println(pathway[0].x);
    System.out.println(pathway[1].x);
    
    
  • The second println would break the program, because it is trying to read x out of null; the second Point object hasn't been created.

Hardwiring

  • We can actually fill them with existing data at declaration:
    
    double xCoords[] = 
      {10.0,0.0,1.0,1.0,2.0,3.0,5.0,8.0,1.0,2.0};
    
    Point pathway[] = {p1, new Point(), p2};
    
    
  • If you are going to do this, the declaration and data filling has to be part of the same command.

Size

  • The size of an array is fixed once made, and can be found using the syntax:
    	
    name.length
    
    
    e.g.
    	
    System.out.println(xCoords.length);
    
    

Objects revised

  • Note also that arrays are a special kind of object, as you can see by the way we make them:
    	
    int xCoords[] = new int[10];
    
    
  • And the fact that they have special variables set up inside them:
    
    xCoords.length
    
    

Multi-dimensional arrays

  • You don't just have to have one dimensional arrays, you can have as many dimensions as you like.
  • A map of population density for example may be a 2D array...
    
    int popMap[][] = new int[100][100];
    
    
  • You can think of the array as a table, with the first size as the row numbers and the second as the columns.
  • You refer to the position as, for example...
    
    arrayName[10][1] 
    
    
    (value at the 11th row, 2nd column)

Multi-dimensional arrays

  • Alternatively you can think of them as arrays of arrays, and there is nothing to stop you setting them up like this:
    	
    int array2D[][] = new int [4][];
    array2D[0] 	= new int[3];
    array2D[1] 	= new int[3]; 		
    array2D[2] 	= new int[3];		
    array2D[3] 	= new int[3];
    
    
  • Each position in the first dimension being filled with a new array.
  • Note that you must always give the size of the first dimension.

Irregular arrays

  • Infact, the dimensions don't have to be regular:
    
    int array2Dirreg[][] = new int [4][];
    array2Dirreg[0] 	= new int[1];
    array2Dirreg[1] 	= new int[3]; 		
    array2Dirreg[2] 	= new int[2];		
    array2Dirreg[3] 	= new int[3];
    
    
  • We can have as many dimensions as we like.
    
    int array3D[][][] = new int[100][][]; 
    
    

Hardwiring

  • We can still fill them with existing objects instead of defining them empty...
    	
    double array2D[][] = 
    {
     {1.0,0.0},{1.0,1.0},{2.0,3.0},{5.0,8.0},{1.0,2.0}
    };
    
    double array2Dirreg[][] = 
    {{1.0},{1.0,1.0,1.2},{2.0,3.0},{5.0,8.0,1.0}};
    
    
  • If it helps, you can break this onto several lines before the semicolon.

Size

  • To find the length in the first dimension, we use:
    	
    name.length
    
    
  • To find the length in the second dimension, we use:
    	
    name[positionInFirstDimension].length
    
    
  • So, for the array on the previous slide:
    	
    array2Dirreg.length  		// is 4
    array2Dirreg[0].length 		// is 1
    array2Dirreg[4].length 		// is 3
    
    

Review

Making a variable is a two-stage process.
Make the name:
	
double myValue;

Assign it to a value:
	
myValue = 23.42;

Or all in one go:
	
double myValue 
        = 23.42;

Making an array is a three-stage process.
Make the name:
	
double [] myValues;

Assign it to spaces
	
myValues = new double[5];

Or all in one go:
	
double [] myValues 
      = new double[5];

Then assign values:
	
myValues[0] = 23.42;