We will add rows with coordinates. This page will explain how to create the characteristics of a coordinate, in other words, how to save x and y. To do this problem you first have to get a class Coordinates.

Content:

  1. Basics First steps to Create an Array
  2. Add Coordinate At The Back

3. Add row at the Back

void addCoordinateAtTheBack(Coordinate coordinate) {
	coordinateArray[numberOfElements] = coordinate;
	numberOfElements++;
}
void addRowAtTheBack(CoordinateRow row) {
	//loop through every element in the row
	//and add one-for-one to the back of the row
}

We already saw how to add one element to a row (in part 1). To add a row you just add every element. We use a loop so we add every element one-for-one.

Illustration: In this example is the value 1, 4, 10 respectively. The boxes represent the array places. For more information about this concept go to the page about arrays.

java programming row

In our program, we call our array: row. So box 0,1,2 exist in the array: row. We have the method: (void) addCoordinateAtTheBack. So we do that for every element.

In words:

  1. addCoordinateAtTheBack(box 0)
  2. addCoordinateAtTheBack(box 1)
  3. addCoordinateAtTheBack(box 2)

But we can’t do it like this, because when we have:

  • a huge array
  • every time another array length

is it not a good solution to do this. So we use our knowledge about loops. What do we know about our array?

  • In this method (addRowAtTheBack), we are working with rows. So our input will not be a coordinate like in part 1. java programming row

We will get a row in our input.

void addRowAtTheBack (CoordinateRow row) {
	....
}

What do we know about a row?

  • We created them ourselves by using the method addCoodinateAtTheBack made in part 1.

How did we do that?

  • We added coordinates to a row using the method (addCoordinateAtTheBack) we made in part 1. We will know the values and numberOfElements.

How do we know numberOfElements?

  • Because every time when we add an element, to the method addCoordinateAtTheBack, the numberOfElements will go + 1.

You could ask for the variables of an object like this:

example: color of the dog will be ==> dog.color;

Because our array is an object. We could ask for the numberOfElements and the value, like this: row.numberOfElements and row.coordinateArray[0], row.coordinateArray[1], row.coordinateArray[2]. How to loop through our elements in our array?

for(int i = 0; i <= row.numberOfElements-1; i++) {
	addCoordinateAtTheBack(row.coordinateArray[i]);
}

So our method will be like this:

void addRowAtTheBack(CoordinateRow row) {
	for(int i = 0; i <= row.numberOfElements-1; i++) {
		addCoordinateAtTheBack(row.coordinateArray[i]);
	}
}

Summary:

void addCoordinateAtTheBack(Coordinate coordinate) {
	coordinateArray[numberOfElements] = coordinate;
	numberOfElements++;
}
void addRowAtTheBack(CoordinateRow row) {
	for(int i = 0; i <= row.numberOfElements-1; i++) {
		addCoordinateAtTheBack(row.coordinateArray[i]);
	}
}