1 / 10

Chapter overview

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.

aferrin
Download Presentation

Chapter overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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]

  4. Two-dimensional arrays: example • See TwoDArray.java • See SodaSurvey.java

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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>

  10. 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

More Related