Sunday, November 18, 2012

11/18/12 Matrices

Two posts ago, I talked about arrays. Did you know that you can make an array of arrays? These arrays will work like a matrix in java. Today I will teach you how to use arrays of arrays or matrices in java.

Think about an array as a line, it exists in exactly one dimension. Now for each point on that line, place a line that is perpendicular to the starting line. Now Image this with arrays instead of lines. This basically gives you a plane filled with data on points, or a matrix. This means that we can view a matrix as simply an array of arrays.

Now that you know this, you may be wondering how you initialize an array. To do this you would type:

(type) [][] (name);

Where type is the data type that you want to use and name is what you want to name the matrix. This will successfully create the matrix, but the matrix will be pointing to null (not there). To get the matrix to point to something, you would type:

(type) [][] (name) = new (type) [(# of rows)] [(# of columns)];

This will create a matrix filled with the number of rows and columns that you want to use. Now that you have the matrix, you probably want to fill it with some data. To add data to a point in a matrix, you would type:

(data you want to add) = (name) [(row)] [(column)]; 

Hopefully you know know something new about matrices (aka arrayception).

No comments:

Post a Comment