arrays and arraylists in java l. kedigh. array characteristics list of values. a list of values...

46
Arrays and ArrayLists in Java L. Kedigh

Upload: elijah-hubbard

Post on 02-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Arrays and ArrayLists in Java

L. Kedigh

Page 2: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 3: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 4: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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

Page 5: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Inspecting a declared array

Page 6: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Creating an Array

• int [] values = new int[6] ;• Default values for integer arrays - zero

Arrays are Reference Data Types – They contain addresses not values

Page 7: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 8: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Declaring, Creating, Initializing

• int [] scores = new int [6] ;• scores = {23, 34, 55, 64, 23, 21};• int[] scores = {23, 34, 55, 64, 23, 21};

Page 9: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Accessing Elements

• int grade = scores[index] ; • where index represents the specific element.

Page 10: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Assume correct declaration of an integer array called numbers.

• Will the following code compile and work as intended?

Page 11: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer

• Yes

Page 12: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Create an array called names that contains 15 Strings.

• Create should (probably) assume the array is already declared.

Page 13: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer(s)

• names = new String [15] ;• or String [] names = new String[15] ;

Page 14: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Initialize an array of integers to the values 31, 32, 33, 34, and 35. Use an array initializer.

Page 15: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer(s)

• someName = {31, 32, 33, 34, 35 } ;• int [] someName = {31, 32, 33, 34, 35 } ;

Page 16: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Write an output statement that outputs the third element in an array

Page 17: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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 18th element of the

array.

Page 18: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Write an output statement that will print the number of elements in an array of integers called numbers.

Page 19: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer

• System.out.println(numbers.length);• Note - NO parentheses

Page 20: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Write an array declaration of integers using the variable name scores.

Page 21: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer

• Key word in the question is “declaration”• int [] scores ;• Note – No size is indicated! We are

declaring, not creating.

Page 22: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Write an array declaration of Strings using the variable name “teams”

Page 23: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer

• String [] teams ;

Page 24: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• What keyword is used to create an array or any object?

Page 25: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer

• new

Page 26: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Question

• Create an array called val that will contain ten integers.

Page 27: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

Answer(s)

• Assuming declaration• val = new int[10] ;

• Not declared• int [] val = new int [10] ;

Page 28: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 29: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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

Page 30: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 31: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 32: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• ArrayList declaration

Page 33: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Creating and adding an element.• In this example I am using the Integer

Wrapper Class – Integer.

Page 34: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Inspecting the ArrayList object

The ArrayList size is 1 – makes sense so far!

Page 35: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Inspecting the data structure in the nums ArrayList shows the array data structure which contains 10 objects, with 9 set to NULL.

Page 36: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Inspecting what is in the first element, index zero.

Page 37: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 38: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Now the first element contains a 99 instead of a 4

Page 39: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 40: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• Executing a set instruction to a location containing a NULL will result in a run-time error or IndexOutOfBoundException error.

Page 41: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• What would happen if we added an eleventh element to this ArrayList?

• Nothing, the ArrayList will grow.

Page 42: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• The initial declaration of the ArrayList allowed for 10 elements.

• When we added an 11th element, the ArrayList added the 11th element and five additional locations.

• The ArrayList size is 11.

Page 43: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

ArrayList Example

• The array data structure inside the ArrayList object has a total of 16 locations.

Page 44: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.

Page 45: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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

Page 46: Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in

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.