Download presentation
Presentation is loading. Please wait.
Published byElijah Hubbard Modified over 9 years ago
1
Arrays and ArrayLists in Java L. Kedigh
2
Array Characteristics List of values. A list of values where every member is of the same type. Each member in an array is called an element. Each array has a variable name associated with it. Each element in an array has an index. Indexing always starts with zero.
3
Array Syntax Declaring – Allocates an array variable. Creating – Allocates space for the elements and fills the array variable with the address of the first element. Initializing – Filling the elements with initial values. Populating – adding elements to an Array.
4
Declaring an Array int [] scores ; Allocates a variable that can “point” to an array of integers Array variables declared but not created have a value of NULL
5
Inspecting a declared array
6
Creating an Array int [] values = new int[6] ; Default values for integer arrays - zero Arrays are Reference Data Types – They contain addresses not values
7
Creating, Initializing and Populating an array After an array is declared: scores = new int [6] ; //create scores = {3,5,6,7,4,3}; // initialize Using a loop with a variable for the index to populate an array.
8
Declaring, Creating, Initializing int [] scores = new int [6] ; scores = {23, 34, 55, 64, 23, 21}; int[] scores = {23, 34, 55, 64, 23, 21};
9
Accessing Elements int grade = scores[index] ; where index represents the specific element.
10
Question Assume correct declaration of an integer array called numbers. Will the following code compile and work as intended?
11
Answer Yes
12
Question Create an array called names that contains 15 Strings. Create should (probably) assume the array is already declared.
13
Answer(s) names = new String [15] ; or String [] names = new String[15] ;
14
Question Initialize an array of integers to the values 31, 32, 33, 34, and 35. Use an array initializer.
15
Answer(s) someName = {31, 32, 33, 34, 35 } ; int [] someName = {31, 32, 33, 34, 35 } ;
16
Question Write an output statement that outputs the third element in an array
17
Answer System.out.println(someName[2]); Third element will have an index of 2. Fifth element will have an index of 4. An index of 17 is the 18 th element of the array.
18
Question Write an output statement that will print the number of elements in an array of integers called numbers.
19
Answer System.out.println(numbers.length); Note - NO parentheses
20
Question Write an array declaration of integers using the variable name scores.
21
Answer Key word in the question is “declaration” int [] scores ; Note – No size is indicated! We are declaring, not creating.
22
Question Write an array declaration of Strings using the variable name “teams”
23
Answer String [] teams ;
24
Question What keyword is used to create an array or any object?
25
Answer new
26
Question Create an array called val that will contain ten integers.
27
Answer(s) Assuming declaration val = new int[10] ; Not declared int [] val = new int [10] ;
28
Array Facts Once an array is created, the number of elements (length) can not change. After an array is created, any element in the array may be accessed in any order.
29
ArrayList A class consisting of a private array data structure It does not require an initial size as the array data structure requires. ArrayList can grow and shrink during program execution. Stores objects only, will not store primitive data (integers, doubles, booleans).
30
ArrayList Wrapper classes are available for primitive data which allows for primitive data to be stored in ArrayLists. The Arraylist method add(object) will add elements to ArrayList data structure.
31
ArrayList set method allows for objects to be placed at specified locations in the ArrayList assuming the location currently contains an object. Any location containing a NULL will not allow the set method to insert an object.
32
ArrayList Example ArrayList declaration
33
ArrayList Example Creating and adding an element. In this example I am using the Integer Wrapper Class – Integer.
34
ArrayList Example Inspecting the ArrayList object The ArrayList size is 1 – makes sense so far!
35
ArrayList Example Inspecting the data structure in the nums ArrayList shows the array data structure which contains 10 objects, with 9 set to NULL.
36
ArrayList Example Inspecting what is in the first element, index zero.
37
ArrayList Example Suppose we want to change the 4 to a 99. We use an ArrayList method called set. The set method requires an index and an object.
38
ArrayList Example Now the first element contains a 99 instead of a 4
39
ArrayList Example An array data structure will allow us to put values at any index at any time. ArrayList allows us to add objects and not specify the location. If the set method is attempted on a location containing a NULL will compile without error.
40
ArrayList Example Executing a set instruction to a location containing a NULL will result in a run-time error or IndexOutOfBoundException error.
41
ArrayList Example What would happen if we added an eleventh element to this ArrayList? Nothing, the ArrayList will grow.
42
ArrayList Example The initial declaration of the ArrayList allowed for 10 elements. When we added an 11 th element, the ArrayList added the 11 th element and five additional locations. The ArrayList size is 11.
43
ArrayList Example The array data structure inside the ArrayList object has a total of 16 locations.
44
ArrayList Example Because ArrayList is a Class we don’t care how the array data structure works inside the ArrayList. We don’t care about NULL elements. All we need to know is how to add element, remove element, and change elements.
45
ArrayList Example nums.add( object ) ; adds to the data structure nums.add(index,object) ; inserts an object into the data structure and moves the current object at index to index+1 as well as moving all elements after index+1. nums.set(index,object) overwrites the object at index
46
ArrayList Example nums.size() returns the number of elements in the ArrayList. NULL is not considered an element in ArrayLists. nums.get(index) returns the object at index. nums.remove(index) removes the object at index and moves all the objects after index up one to fill in the removed object.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.