100 likes | 130 Views
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. one dimension. two dimensions. Two-dimensional arrays.
E N D
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
one dimension two dimensions 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 columns
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]
Two-dimensional arrays: example • See TwoDArray.java • See SodaSurvey.java
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
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
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
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
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>
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