A matrix is basically an 'array of arrays' in Java. Matrixes can be used to store data in a more of an efficient way while coding. In order to declare a matrix object, you would write: The 2 in the first bracket represents the number of rows and the 3 represents the number of columns. So what does this matrix represent? Lets first initialize arbitrary values in this matrix: By initializing this matrix, we have created this matrix (like the image above). There are three rows and three columns, and the numbers which are chosen are displayed in their specific locations as well. So if we would want to access one of these locations/number, we would simply display it: So this statement would display the number 9. We can use matrixes to store multiple things, such as names, student's GPAs, or even age. But, what if we wrote a statement like this: This statement would indicate an error, an out of bounds error since there is no column 3 in the matrix. That column doesn't exist. I hope you were able to learn some things about matrixes, and hopefully you're planning to use them in your code soon! Good luck!
0 Comments
|
Archives
March 2021
Topics
All
|