+rep for you! Come back, one day!
Thread: Multidimensional Arrays |
Multi-dimensional Arrays
This is a tutorial about multi-dimensional arrays, however a quick review of 1-dimensional arrays. The understanding of a 1-dimensional array is crucial to understanding 2 and 3-dimensional arrays.
When you first learn about arrays in a programming language, you think of a bunch of blocks. That are containers for similar variables. This is a 1 dimensional array.
That is a representation of a 1-dimensional array that can hold 6 elements. With arrays you can access items by position not content. Thus the array is the basic data structure that is used in the implementation of more complicated data structures like linked structures and hash tables.
In Java and other languages this array is declared like this:
If you are used to C++ you might see this written as:Code:int[] arnNums = new int[6]; // an array that can hold 6 elements
Both of these forms are acceptable, use whichever you would like.Code:int arnNums[] = new int[6];
Populating the array
You can also use a loop to populate the array.Code:int[] arnNums = {0,1,2,3,4,5}; // declaring and initializing an array to hold 6 elements
Example:
You can access how many elements are in the array by using the length property.Code:for (int i=0;i<6;i++) { arnNums[i] = i; }
Before you continue try this problem to make sure you have this concept down.Code:arnNums.length; // returns 6 since there are 6 elements in the array.
Two-dimensional Array1. Have the user input 10 numbers. Positive and negative numbers are allowed. The numbers will all be real numbers. With this set of 10 numbers you want to calculate:
a) Integer Mean (Don't use reals)
b) Median
c) Largest number
d) Smallest number
e) Number of even numbers
f) Number of odd numbers
A two dimensional array is really just an array of an array. So each element in the array is an 1-dimensional array. Think of a 2-dimensional array as a grid like a chessboard.
Example:
This diagram is showing how each element has 2 indexes not 1. This is what makes it a 2-dimensional array.
Remember that is really just an array of arrays. The first number says what sub array the element is in, and the second index tells the exact number in the array.
You can populate a 2d array using the brace bracket notation in the same manner as a 1 dimensional array.
This diagram shows what value is located at each position in the array. So there is a 5 at [0,0], [0,1], [0,2], etc.
The above array can be constructed like this:
Since each element of the array is really just another array. We can get the number of elements in each individual sub array by using:Code:int[][] arnGrid = { {5,5,5,5,5,5}, {5,5,5,5,5,5}, {4,3,3,2,1,-5}, {2,2,2,5,2,3} };
We can also get the number of sub arrays by using:Code:arnGrid[0].length
This allows us to perform a loop through the entire array printing the grid.Code:arnGrid.length
So the outer loop executes once for each sub array in the 2-dimensional array. Then the inner loop executes once for each element in the sub array. The output is:Code:for (int i=0;i<arnGrid.length;i++) { for (int x=0;x<arnGrid[i].length;x++) { System.out.print(arnGrid[i][x] + " "); } System.out.println(); }
So can you get the user to input numbers and store them in the array?5 5 5 5 5 5
5 5 5 5 5 5
4 3 3 2 1 -5
2 2 2 5 2 3
Note that you can also declare a 2-dimensional array like this:
This means that there will be 5 subarrays which will each hold 6 elements.Code:int[][] arnNums = new int[5][6];
This can also be written in C++ style as:
Before we move on any farther let's try a few challenges:Code:int arnNums[][] = new int[5][6];
Navigating 2-Dimensional Arrays1. Create a multiplication table. Have the user enter two numbers 5 <= x <= 7 and 7 <= y <= 9
The program will create a multiplication table from 0*x-1 to x-1*y-1. Make sure you get the alignments correct.
For example:
x = 3
y = 5
I want to create a table from 0*3 to 2*4:
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
Assume that you are required to store the multiplication table for later use.
2. Have the user enter a 2-dimensional array of numbers and determine if it's a magic square. The user will enter 2 numbers x and y which can be at maximum 50,000. A magic square is a grid where all the rows, columns and diagonals add to the same number.
I like to keep two arrays one array that holds information on how I can move left or right in the grid, and another array that holds information on how to move up or down. These are two integer arrays that can only hold 1, 0, or -1. 0 means that I am not moving in that direction. 1 means that I am moving either left or up. -1 means that I am moving down or right.
Example:
This covers the four main directions: "north", "east", "south" and "west". To move north 1 in the array I add 1to the "y" coordinate and x doesn't change. To move east I add 1 to the x coordinate and y does not change. This covers moving in the four main directions. Now I can easily move in north-east and other directions similar to that.Code:int[] x = {0,1,0,-1}; int[] y = {1,0,-1,0};
A for loop allows me to loop around a current square in all directions.
Note that it is very important to reset the coordinates of where you are every time. Also bounds checking is important. We don't want to check if a square is a mine if it is not in the grid.Code:for (nDir=0;nDir<4;nDir++) { dx = x; // current x coordinate dy = y; // current y coordinate dx += x[nDir]; // changing x coordinate to one of the squares around [dx][dy] dy += y[nDir]; // changing y coordinate to one of the squares around [dx][dy] // now it is very important to make sure we have not gone out of bounds if (dx < 0 || dx > 5 || dy < 0 || dy > 6) { // this is out of bounds so we don't go any further continue; } // in bounds so continue processing here }
A quick note in Java, a string is an array of characters so an array of strings is a 2d array.
The 2-Dimensional Array comes in handy when implementing graphs.
Now have some fun with 2-dimensional array problems:
http://dwite.ca/old/Problem2Jan2005.pdf
http://dwite.ca/old/Problem3Nov2002.pdf
http://dwite.ca/old/Problem4Oct2002.pdf
http://dwite.ca/old/Problem4Oct2005.pdf
It might be fun to talk about my various challenges and the DWITE questions.
James
Some resources:
Arrays (Java 2 Platform SE v1.4.2)
Multi-Dimensional Arrays
Javanotes 5.0, Section 7.5 -- Multi-dimensional Arrays
Last edited by chili5; 03-17-2009 at 12:07 PM.
"Whenever you remember, I'll be there/
Remember how we reached that dream together" - Carrie Underwood
+rep for you! Come back, one day!
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Very informative tutorial! +rep.
Sweet![]()
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein
Great thanks everyone!
What do you mean "Come back, one day!"?
"Whenever you remember, I'll be there/
Remember how we reached that dream together" - Carrie Underwood
It's a reference to the "Soup Nazi" Seinfield episode. Basically: I expect a new tutorial tomorrow.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Not off hand... 4 dimensional arrays?
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Sure sounds good.It would be good to write about something I don't already know about.
![]()
Yet another excellent tutorial of yours mate. They are always so highly detailed and understandable even thought I don't know Java.
Thank-you. +rep
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
There are currently 1 users browsing this thread. (0 members and 1 guests)