In this IB Computer Science lesson you will be learning about:
Recall that an array is like a long list of variables of the same type, but handles the naming of those variables in a nice, compact way. Arrays in combination with loops are a very powerful tool in programming and allow more complex problems to be solved
Array Fundamentals
It is best to view an array as a list of variables which has a 2 part name
Before an array can be used in Java, it must be declared, just like any other variable must be declared. Like strings, index values start at 0 for the first variable of the list
To declare an array in Java, you must indicate the type of values that array will store, an identifier for the name of the array, and how many values that array can store. Arrays can be any type of variable we have previously discussed and later you will learn how to create an array of objects
The syntax for an array declaration is as follows:
type [ ] identifier = new type [ size ] ;
In the example above 3 arrays were created called: values, temperature, name. By making those declarations, space has been reserved in memory for the specified number of variables
For example, in the second example an array called temperature consisting of 5 variables all of type double, and named :
temperature [ 0 ], temperature [ 1 ] , temperature [ 2 ], temperature [ 3 ], temperature [ 4 ]
The numbers in the square brackets are called the index of the array. temperature [ 0 ], temperature [ 1 ] . . . etc. are called the elements of the array
There are several different ways to assign values to array elements
We will first look at how to assign values at the time of declaration
Looking at the second example again, the following values were assigned to the elements in the array
temperature [ 0 ] = 7.4, temperature [ 1 ] = 3.5, temperature [ 2 ] = -5.1, temperature [ 3 ] = 12.4, temperature [ 4 ] = 28.5
Array elements can also be assigned values at any point in the program. The following program assigns a random number to each value for an array
The following example can be used to ask the user for the names and ages of 5 people and store them in memory. The program then calculates the average age and displays the spread of data in a nicely formatted way on the screen.
Here is a program that generates 15 random integers from the user and stores them in array. It will display all the numbers entered on 1 line and count how many numbers are less than 25, and display that result on the screen.
IMPORTANT -> Java passes all primitive data types such as int, char, double and even Strings by
value, however arrays and objects are a different matter. These are passed by reference.
When java passes something by reference it basically passes the memory address of the argument,
so any modification made to that memory address will be reflected in the rest of the program
Example
Here is a method that makes all the values in an array equal to zero
Before the method is called the data stored in the array is a bunch of random numbers. After the method is called the data stored in the array is a bunch of zero’s.
There was no need to return a new array since the array b just pointed to the memory location of the passed array values, so when you made modifications to array b, it changed the data in that location which array a was already pointing to back in your main method.
Be careful with objects and arrays that are passed by reference, it could make your programming
easier but it may be the source of why your program is not working as expected.
1. Dice Rolls
Create an array named diceRolls. This array has 100 integer elements. Initialize each element with a random number representing the roll of a die. The die has the possible rolls of 1, 2, 3, 4, 5, or 6.
2. Names & Addresses
You are going to be storing names and addresses of people in 2 separate arrays. To make testing easier, define all the values inside your program (don’t ask the user to input them). Have 5 different names and corresponding addresses. Write a menu program that allows the user to choose the following options:
3. Statistics
Generate an array of 40 random decimal numbers. Write methods to accomplish the following tasks.
4. Rotating an Array
Write a method that can shift an array by N spaces. A positive number will shift the array down. Values that go off the end of the array will wrap back to the beginning. The user should be able to enter positive or negative values for the shift. Output the array before the shift and after the shift.
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page