Download presentation
Presentation is loading. Please wait.
Published byDale Clark Modified over 9 years ago
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
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 one dimension two dimensions
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 ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer
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
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 reunion = new ArrayList
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 1 st occurrence of the specified object
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.