levo

Objects, Classes and Data Structures

Arrays of objects

  • 3 things to know
  • AS Unit 1

Overview

Declaring an array whose elements are objects, why every element starts empty, creating each object before it is used, and the error raised by reaching through an element that holds nothing.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe how the object with the highest value of an attribute is found in an array of objects.

Key term

A library program declares an array of 200 Book references but currently holds only 40 books. What is the cost of that decision?

Key term

What has to be done to an element of an array of objects before it can be used?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does the statement Student[] group = new Student[3]; create?

    Model answer

    The statement creates an array able to hold three Student references, and no Student object exists yet because every element starts as null.

  2. 2

    A program executes Student[] group = new Student[3]; and then System.out.println(group[0]);. What is printed?

    Model answer

    The statement prints null, because creating the array reserves three reference slots and sets each of them to refer to no object at all.

  3. 3

    What has to be done to an element of an array of objects before it can be used?

    Model answer

    Each element must be given an object of its own, as in group[0] = new Student("Aoife");, before any method can be called through that element.

  4. 4

    A program executes Student[] group = new Student[3]; and then group[0].getName();. What happens?

    Model answer

    The program stops with a null pointer exception at run time, because element 0 still refers to no object and there is nothing on which getName could be called.

  5. 5

    What does each element of an array of objects actually store?

    Model answer

    Each element stores a reference to an object held elsewhere in memory rather than the object itself.

  6. 6

    Contrast what is stored in an array of integers with what is stored in an array of objects.

    Model answer

    An array of a simple type holds the values themselves inside the array, while an array of objects holds references and the objects sit separately in memory.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Objects, Classes and Data Structures, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.