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

Post on 02-Jan-2016

228 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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.

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

Inspecting a declared array

Creating an Array

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

Arrays are Reference Data Types – They contain addresses not values

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.

Declaring, Creating, Initializing

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

Accessing Elements

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

Question

• Assume correct declaration of an integer array called numbers.

• Will the following code compile and work as intended?

Answer

• Yes

Question

• Create an array called names that contains 15 Strings.

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

Answer(s)

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

Question

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

Answer(s)

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

Question

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

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.

Question

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

Answer

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

Question

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

Answer

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

declaring, not creating.

Question

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

Answer

• String [] teams ;

Question

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

Answer

• new

Question

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

Answer(s)

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

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

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.

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

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.

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.

ArrayList Example

• ArrayList declaration

ArrayList Example

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

Wrapper Class – Integer.

ArrayList Example

• Inspecting the ArrayList object

The ArrayList size is 1 – makes sense so far!

ArrayList Example

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

ArrayList Example

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

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.

ArrayList Example

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

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.

ArrayList Example

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

ArrayList Example

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

• Nothing, the ArrayList will grow.

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.

ArrayList Example

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

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.

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

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.

top related