ICS 4U1 - Unit #3 - Two Dimensional Arrays - Dec 7th
ICS 4U1 - Unit #3 - Two Dimensional Arrays - Dec 7th
Multidimensional Arrays
Revisiting One Dimensional Arrays
Two Dimensional Arrays Fixed Arrays
and
Dynamic One Dimensional and Two Dimensional Arrays
What are we doing now?
• Earlier this year (and late last year), you were introduced to the concept of one-dimensional arrays.
• We started with fixed size arrays and were not allowed to grow past that size. We then proceeded to create
methods that would permit the arrays to grow dynamically at run time. We will extend this further in this
unit!
• One dimensional arrays are great to store a list of numbers or objects, but what if we want to store a matrix
of numbers or objects ? What is a matrix you ask?
• A matrix is simply a representation of rows and columns of some objects or numeric values. Think of a
classroom that is organized with desks that are lined up in rows and columns. That is a matrix.
• Many problems lend themselves to storing data in a matrix rather than a one-dimensional array. Think of
some common board games – battleship, chess, tic-tac-toe, connect four. If you were to write a computer
program to store the state of the board and act upon it you would use a two-dimensional array in your
solutions.
• A rite of passage as a student learning to write computer programs is to code the game Tic-Tac-Toe. In this
unit, we will be doing so first with a one-dimensional implementation since you have not done so in the past
and will also infuse object-oriented programming techniques in our solution. Then we see how a two-
dimensional array solves the problem a bit more elegantly and naturally.
One Dimensional Implementation of Tic Tac Toe
• Let’s start with a one-dimensional String array:
Super important:
Initializes to default values as in
previous unit. Check out the
following code here:
https://onlinegdb.com/XMc56FXhE
Special note …
• When you initially declare a 2D array using the keyword new
• You must always specify the first dimension!
• If you are creating a 3D array, you do not need to specify the second or third
dimension.
• If you know both dimensions that of course is ideal. Just specify them when
creating the array.
• Do NOT worry about the 2D array growing, as we will handle that with a
Dynamic Arrays class as it will get difficult to manage otherwise by hand but
think on how one would do that. Remember our resizing of the array code?
How do we print out the contents of an array?
• The Arrays class has a fancy built-in, but it is important to try and build
our own, as it will show us a few new tricks J.
• Let’s adjust our previous one-dimensional toString() method that acts on
a one-dimensional array. https://onlinegdb.com/kO7Ty9_DB )
https://onlinegdb.com/AgFEW-OlP
Output:
https://onlinegdb.com/rbAQwoudTn
Solution …
• Here is my solution … take a moment to dissect it to see what new tricks you
can learn J
Solution …
• Here is my solution … take a moment to dissect it to see what new tricks you
can learn J
Solution …
• Take a moment to dissect it to see what new tricks you can learn J
https://onlinegdb.com/BupJqN0zP
Ragged or Jagged Arrays
• In each of the examples we have looked at so far, we have worked with
rectangular two-dimensional arrays.
• Java allows us to create and work with what are called ragged/jagged
arrays which are not rectangular and have different sized rows and
columns.
• Sample ragged or jagged arrays:
But how do we do this?
• Let me show you … lettuce go back and examine what happens when
you create a rectangular array using the code on the left and compare it
to what is its equivalent on the right:
• Let’s play a bit with this ragged array and write s’more code to do
something.
Here is some simple code:
• The above code could be used to store the following ragged two-
dimensional matrix of integers:
We can also …
• Should we need to fill the array at compile time we can do so using the
following:
• Each row could represent say a student’s earnings for a week, with each of
the entries within that row representing week 1, week 2, and so on.
Obviously if we are going to talk about earnings, we should use double 😜.
• We won’t be doing any assignment with ragged/jagged arrays. Just be aware
that they can be defined and can solve a problem when you don’t want a
rectangular array. Besides, it may be easier to use dynamic arrays to
implement a ragged array.
Mini-Problem
• Write the code necessary to setup the following
Jagged array of type String.