PracticeSubscribe

Lessons / Computer Science / IB Computer Science | Year 2 (Senior) / 2D Arrays in Java · part 1/8

Computer ScienceIB2 · SeniorB2.2

2D Array Java Explained: Rows & Columns

2D Array Java Explained: Rows & Columns

What you'll learn

IB Computer Science B2.2, 2D arrays in Java: rows, columns and indexes (part 1 of 8, for SL and HL). What a 2D array is in Java, how to create one, and how to read and change one element. With a 5-mark exam question solved step by step.

In this lesson

  1. 0:00Intro
  2. 0:17Exam question
  3. 0:39Warm-up
  4. 1:21Rows and columns
  5. 2:28Creating a 2D array
  6. 3:37Read and change
  7. 4:22Tracing practice
  8. 5:53What earns the mark
  9. 6:26Exam question solved
  10. 7:20Recap

Practice questions

  1. State the line of code that creates a 2D array of doubles named temps, with 7 rows and 24 columns. [1]
  2. int[][] m = { {2, 4, 6}, {1, 3, 5} };
    State the value of m[1][0] + m[0][2]. [1]
  3. Outline why m[2][0] causes an error. [2]

Original questions in the style of the exam. Try them after the video, then check the answers below.

Answers to the practice questions
  1. double[][] temps = new double[7][24];
  2. 7 (1 + 6).
  3. m has only rows 0 and 1, so there is no row 2 [1]; the index is out of bounds and the program stops with a run-time error [1].

Try them at the end of the video first. Questions are original, in the style of the exam; marks are estimates.

Read the lesson (full transcript)

Hi! I'm the IB Axolotl. This is two D arrays in Java, part one of eight.

Today you will learn three things. What a two D array is. How to create one. And how to read and change one element.

Here is a five mark exam question. In seven minutes you will answer every part, and you will know where each mark comes from.

Two D arrays are for standard level and for higher level, so all eight videos are for everyone. If you already know this part, jump to the chapter called "Exam question solved".

First, two questions about normal arrays. An array called a holds five, eight and two. What is a, index one?

Eight. Counting starts at zero, so index one is the second element.

Second. An array has six elements. What is its last index?

Five. The last index is always the length minus one. Keep that rule. We need it today.

A normal array is one line of elements. A two D array is a grid of elements. Rows go across. Columns go down.

Think of a cinema. To find your seat you need two numbers: the row, and then the seat in that row. A two D array works the same way. Every element has two indexes. The row index first. The column index second.

And both indexes start at zero. This grid has two rows, so the rows are zero and one. It has three columns, so the columns are zero, one and two.

Careful here. It is always row, then column. If you write the column first, a square grid still runs, and it gives you the wrong answer with no error message.

Small task. What is the value of grid, one, two?

Five. Row one is the second row. Column two is the third column.

Here is how to create one. Int, two pairs of square brackets, the name, then new int with two sizes. The first size is the number of rows. The second size is the number of columns.

This line makes three rows and four columns. That is twelve elements. Java gives every element a starting value. For int, zero. For double, zero point zero. For boolean, false. For String, null, which means "nothing here yet".

If you already know the values, write them in curly brackets. One inner pair of curly brackets for each row.

Careful here. The sizes are three and four, but there is no row three and no column four. The last element is row two, column three.

Small task. New int, two, six. How many elements?

Twelve. Two rows times six columns.

To read an element, write the name, the row and the column. Here x gets the value five.

To change an element, put it on the left of the equals sign. Grid, zero, one, equals nine. The seven becomes nine.

Careful here. On the left of the equals sign, the element is changed. On the right, it is only read.

Small task. After that change, what is grid zero one, plus grid one zero?

Seventeen. Nine plus eight.

Reading code one line at a time is called tracing, and it earns marks. We practise three times.

I trace this code with a table. Line one makes a grid with two rows and three columns, full of zeros. Line two stores four in row zero, column two. Line three reads that four, adds one, and stores five in row one, column zero. Line four reads the five, doubles it, and stores ten in row zero, column two. The four is gone.

Now together. This grid has three rows and two columns. I find g, two, zero. Row two is the last row. Column zero is the first column. Seven. You finish: find g, zero, one, and add.

Six. Seven plus six. The code prints thirteen.

Now alone. This code should put eight in the last element, but it crashes. Find the mistake and fix it. Pause the video.

The grid has two rows and three columns, so the last element is row one, column two. Not two, three.

A two mark question and three answers. Decide the marks. Five seconds.

Student A only gave the name. Zero. Student B gave the number of rows and columns. One mark. Student C also gave the type and the starting value. Two marks.

So when you describe an array, give three things: the type, the size, and the starting values.

Back to the question from the start. Pause the video and try all four parts.

Part a. "State" means give the answer, with no explanation. Three rows times five columns: fifteen elements.

Part b. Nothing was stored in row zero, column three, so it still holds the starting value. Zero.

Part c. Row one, column four holds seven. Seven plus two is nine.

Part d. "Outline" means a short account. The rows are zero, one and two, so there is no row three. One mark. The index is out of bounds, and the program stops with a run-time error. One mark.

Five out of five.

One screen. A two D array is a grid. Every element has two indexes: row first, then column, both from zero. New int, rows, columns creates it, full of starting values. And an element on the left of the equals sign is changed.

Three questions. Try them today, and again in three days. The answers are in the description. Next, part two: how to visit every element with two loops. See you there!

Practice this topic

IB Computer Science B2.2 Data structures: questions and flashcards

Original exam-style questions with markschemes. Free, no account.