Objectives

Students will be able to:

  • Declare and initialise ArrayLists of objects
  • Use ArrayList methods such as add, set and remove to process ArrayLists

ArrayList Challenge

dynamic vs static

Static Arrays

Static arrays have a fixed length.

In order to process them, we need to understand how to use indexes eg.

Apple[] apples = new Apple[100];

//code to populate the array with Apple objects not shown

int i = 0;

while(i<apples.length && apples[i]!=null){

Apple a = apples[i];

if(a.getWeight()>120)

apples[i]=null;

}

i++;

}

When processing a static array of objects, we often need to understand that a null pointer may exist.

If not handled, this can cause a .

ArrayLists

ArrayLists are dynamic (its size can change during program execution). In order to process an ArrayList, we need to understand how to use ArrayList class methods eg:

ArrayList<Apple> apples = new ArrayList<Apple>();

//code to populate the array with Apple objects not shown

int i = 0;

while(i<5){

apples.add(new Apple());

i++;

}

for(int i = 0; i<apples.size(); i++){

Apple a = apples.get(i);

if(a.getWeight()>120){

apples.remove(i);

i--;

}

}

When processing an ArrayList of objects, we don't need to care about null!

However, when removing objects we need to understand that the indexes of the ArrayList will update dynamically and this may cause some problems.

Task

Look back at the course so far and choose a programming unit that required a solution involving static arrays.

Revisit this unit and redesign the solution to utilise ArrayLists instead of arrays.

Some options are:

A Tank has an array of Missiles. Redesign the Tank class so that it has an ArrayList of Missiles.

What changes do you need to make to the shootMissile() method of the Tank class?

And how about the getAverageRating() method of the Tank class.

Create an ArrayList of Tanks in a Main program and start a war!

Try to use as many ArrayList class methods as you can in your program:

  • .size()
  • .get(index)
  • .add(Object)
  • .add(index, Object)
  • .set(index, Object)
  • .remove(index)

A Ufo has Aliens. Revisit this unit using ArrayLists.

Task 2 is interesting. Using static arrays we returned null if we couldn't find the object in the array. What would we return when using ArrayLists? Be creative!

Task 3 is also very interesting. Is it possible to order an ArrayList of Alien objects in order of age?

Try to use as many ArrayList class methods as you can in your program:

  • .size()
  • .get(index)
  • .add(Object)
  • .add(index, Object)
  • .set(index, Object)
  • .remove(index)

Remember those Files, Folders and Lines? Revisit this activity and generate a solution using your knowledge of ArrayLists.

Try to use as many ArrayList class methods as you can in your program:

  • .size()
  • .get(index)
  • .add(Object)
  • .add(index, Object)
  • .set(index, Object)
  • .remove(index)

Reinvent JeepSheep! Revisit this activity and generate car race using your knowledge of ArrayLists.

You could use your Java Swing skills to turn this into a visual game, too (optional but recommended)

Try to use as many ArrayList class methods as you can in your program:

  • .size()
  • .get(index)
  • .add(Object)
  • .add(index, Object)
  • .set(index, Object)
  • .remove(index)

Tags

static.add(index, Object) fixed-length dynamic .set(index,Object) .size() .remove(index) .add(Object)indexesnull