Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity...

10
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter lists Multidimensional arrays The ArrayList class

Transcript of Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity...

Page 1: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Chapter overview This chapter focuses on

Array declaration and use

Bounds checking and capacity

Arrays storing object references

Variable length parameter lists

Multidimensional arrays

The ArrayList class

Page 2: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Two-dimensional arrays

A one-dimensional array stores a list of element

A two dimensional array Can be thought of as a table of elements

With rows and columnsone

dimensiontwo

dimensions

Page 3: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Two dimensional arrays: declaration To be precise, in JAVA

A two-dimensional array is an array of arrays

A two-dimensional array is declared by specifying the size of each dimension

An array element is referenced using two index values

The array stored in one row Can be specified using one index

int[][] scores = new int[12][50];

value = scores[3][6]

Page 4: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Two-dimensional arrays: example

See TwoDArray.java

See SodaSurvey.java

Expression Type Description

table int[][] 2D array of integers, or

array of integer arrays

table[5] int[] array of integers

table[5][12] int integer

Page 5: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Chapter overview This chapter focuses on

Array declaration and use

Bounds checking and capacity

Arrays storing object references

Variable length parameter lists

Multidimensional arrays

The ArrayList class

Page 6: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

The ArrayList class

The ArrayList class is part of java.util

Can store a list of values and reference each one using a numeric index

Dynamically grows and shrinks as needed Adjusting its capacity as necessary

However, you cannot use the brackets syntax With an ArrayList object

Page 7: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

ArrayList elements

ArrayList is not declared to store a particular type

any type of object can be added to an ArrayList

stores references to different types of objects

If a primitive value must be stored in an ArrayList Use the appropriate wrapper class

Page 8: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Arraylist: inserting and removing elements

Elements in an ArrayList Can be inserted or removed

with a single method invocation

When an element is inserted Other elements move aside to make room

When an element is removed The list collapses to close the gap

The indexes of elements adjust accordingly

Page 9: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Specifying an ArrayList element type

See Beatles.java

we can also define An ArrayList object to accept a particular object type

The following declaration creates an ArrayList object that only stores Family objects

ArrayList<Family> reunion = new ArrayList<Family>

Page 10: Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.

Methods defined in ArrayList

boolean add(Object obj) Inserts the specified object to the end of the list

object remove(int index) Removes the element at specified index in the list

void add(int index, Object obj) Inserts the specified object into list at specified index

int indexOf(Object obj) Returns the index of 1st occurrence of the specified

object