Arrays in Java work similar as we have discussed in pseudocode. They are linear, they have indexes, they cannot change in size.
The length or size of the array is the number of elements, in the case of numbers
is 6.
The index of 3
is 0
.
The value of numbers[2]
is 522
.
The maximum index is always equal to [nameOfThArray].length -1. In this case is 5, whose value is 9.
We can also not now the values of the numbers:
This is an arry of 399 elements.
We usually use for loop this way:
There is the possibility of using for each.
I suggest don't use it unless you're too deep into python
More reference on this:
https://www.w3schools.com/java/java_foreach_loop.asp
It's very common in classes that they have an array of Objects instead of primitive.
For example, makes sense that a IBClass should have an array of IBStudent
In IB you will find scenarios where they use arrays. In this scenarios they need to handle sometimes that the number of objects is variable, for example the number of students in a subject may vary from subject to subject. If we have an AirCompany, it may have a variable number of Planes in their fleet. So to deal with that you will find counter variables.
In these cases you will have an array of objects that it's over the limit the maximum size of the actual model (so the program doesn't get out of elements). For example in the class "Subject" we might find an array of 50 students (Because 50 is already too much in IB), if we talk about a parking lot we might have an array of Cars that are entering that is just the size of the parking or in the case of a fleet of an air company we can go with 200 planes (that's already a big number).
Counter variables are int that they are related to an array and count how many elements we have in that oversize array.
For example:
This counter variable sometimes has an accessor but it shouldn't have a mutator.
We don't usually have the array as a variable in the constructor, but you might find it.
We don't have direct accessors and mutators of the array. You won't see, in the case of AirCompany methods like "getFleet" or "setFleet". This is because we want to make sure that AirCompany has full control of fleet and also the counter variable.
This accessor is different from regular accessors because they require a parameter. The index of the element that we're looking for. They have to return the object.
This is different depending of the context. I've seen this as part of the difficult question to implement (7 marks). The idea can be "find one element, then take it out, reorder the rest of the elements so there is no extra space, update the counter".
If they ask this in an exam the will give you the specific instructions to construct the method.
The easier way is the input is the index.
Construct the method removePlane(index) in AirCompany that given a specific index, if it's valid it's going to update the number of planes and create an array that keeps the same order and returns the deleted Plane.
Quite a mess, huh?
Let's do the next twist
Remove a plane given a specific attribute (the first one that we find)
Construct the method removePlane(index) in AirCompany that given a specific modelName, if it's valid it's going to update the number of planes and create an array that keeps the same order and returns the deleted Plane.
Another way of implementing the same method. Both are valid but the second one is more complicated to understand and it's a bit more oneliner
Sometimes one of the methods that you can find in an class that has an array (or any given collection) is a "finder". A method that tries to finde in that array a specific element that complies with a specific characteristic. This types of methods usually will have the parameter (or parameters) the finding criteria and they usually return the object itself. This is specified in the statement.
They usually loop through the array and inside has an if clause with the specific condition that needs to meet.
Construct the method findPlaneByPassengerCapacity() that given a given passengerCapacity returns the first plane in the fleet that complies with that value.
Construct the method findPlaneByPassengerCapacity() that given a given passengerCapacity returns the first plane in the fleet that complies with that value.
A specific version of the "finder" methods where you count elements that has a specific propierty. The idea is that you initialize the counter, you loop through the array, you add 1 to the counter then you return the counter.
Construct a method planesAvailable in the AirCompany that counts how many planes in their fleet are active at any given time and returns that value.
This is very common in exercises in OOP exams. They give you the whole code and they say you that in the driver or main class you will have some code. Then your task is to be able to know what would be the output.
Output
0
2
Concorde
true
Output:
3
5
Airbus
I suggest to use one of the scenario and tweak it and try to see what happens using the compiler.
These are the classes that I'm using as context here during May2025
Main.Java:
AirCompany.java:
Plane.java:
https://www.codesdope.com/practice/java-array/
Nice option with the olutions
http://www.eecs.qmul.ac.uk/~pc/teaching/introprogramming/week7/exercises7.html