64
A+ Computer Science LISTS

A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science -

www.apluscompsci.com

A+ Computer Science

LISTS

Page 2: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

What isa List?

© A+ Computer Science - www.apluscompsci.com

Page 3: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Problems with Arrays:What are the limitations of Arrays?

Page 4: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Problems with Arrays:● Hard to add elements (need to existing bump elements up)● Hard to remove elements (need to bump existing elements down)● Hard to replace an element● Hard to find an element● Have to import and use Arrays.toString to print the array values

Page 5: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

So JAVA creators made the ArrayList class

● It builds on basic arrays by adding helpful methods ● It’s a fancy class of Arrays that stores Objects● They wrote the code so you don’t have to!

Page 6: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

If Arrays and ArrayLists were Fords

Arrays are a manual Ford Fiesta...they get you where you need to be, but you need to do a lot of the work...roll up the windows, dial the radio, shift the gears etc.

ArrayLists are an automatic Ford GT. They also get you where you need to be, but have a lot of nice, built-in features that you can rely on.

Page 7: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

What isa List?

© A+ Computer Science - www.apluscompsci.com

Page 8: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Stacey Got a Little Ahead of Himself.Let’s do a quick preview lesson on:

REFERENCE VARIABLES(Sometimes called “Pointers”)

Page 9: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Primitive variables carry their values with them like color coded sticky notes

int age = 18;

Every time I declare a variable, a new stickynote is created with its name and value all togetherat the same memory location

age 18

Page 10: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

We are about to learn that sometimes the name is separated from the value. The name POINTS to the value

age

Integer age = 18;

18Ox8d65a

● age “points” to the memory location where the value is stored.

● Age acts like a POINTER● Age does not know it is 18● Age only knows where to POINT

Page 11: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Wrapper Class

ArrayLists MUST store Objects, or Pointer variables. They cannot store primitive types. For this reason, Java writers made ‘Wrapper Classes” to emulate primitive variables.

age

age

Wrapper Class for ArrayList Primitive Type

Integer Int

Double Double

Boolean boolan

Page 12: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

● Arraylist is a class that houses anarray.● An ArrayList can store any type.● All ArrayLists store the first

reference at spot /index position 0.

Now - ArrayList

Page 13: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

Array vsArrayList

● When you initialize, you tell the CPU how much memory you will require. For example:

○ Enough for 7 ints○ Enough for 10 Strings○ Enough for {19.0, 21.5, -6.4}

● All of the data is stored TOGETHER.

● You can’t ever store more or less

● When you initialize, you make ONE reference pointing to NOTHING. You tell it what TYPE of Object it expects to point to next.

● When you add a new object, it becomes part of the list

● Then it expects to POINT to the next object

● This forms a LINKED list● The values are stored all over the

computer’s memory

Page 14: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

0 1 2 3 4 5 6 7 8 9

nums 0 0 0 0 0 0 0 0 0 0

int[] nums = new int[10]; //Java int array

An array is a group of items all of the same type which are accessed through a single identifier.

Array - fixed in memory

Page 15: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

aplus

nothing

null

null

ArrayList aplus;

aplus is a reference to an ArrayList.

ArrayList - Dynamic memory

Page 16: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Generics

© A+ Computer Science - www.apluscompsci.com

Page 17: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<String> words;words = new ArrayList<String>();

List<Double> decNums;decNums = new ArrayList<Double>();

ArrayList - Declaration

Note that ArrayLists must contain OBJECTS because of their ability to “point” to other obejcts. So instead of double, we use Double. Instead of int, we use Integer. These are called “wrapper classes.”

Page 18: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Long> bigStuff;bigStuff = new ArrayList<Long>();

List<It> itList;itList = new ArrayList<It>();

ArrayList

We are working towards being able to build our own OBJECT Types to store in ArrayLists. In this example, Stacey built his own “It” class of Objects

Page 19: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<String> vals;vals = new ArrayList<String>();vals.add("aplus");vals.add("compsci");vals.add("contests");out.println(vals.get(0).charAt(0));out.println(vals.get(2).charAt(0));

ray stores String references.

OUTPUTac

Page 20: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Hands’ On Learning Time! To Do now and for Homework #41) Learn About Array Lists by looking at the Oracle JAVA

doc.2) Work through the white worksheets about ArrayLists3) Start Netflix! Open up a new class in Labs 16_ArrayLists

Page 21: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Answers to ArrayLists

Hobbits: Gandalf, Bilbo, Fili, Thorin, KiliVillains: Smaug, Sauron

false0

Elves: LegolasThranduilArwen

there is no element at index 8Nums: 975313579

975313579nums.removeAll(9)

Page 22: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

Answers to Collections

Ages: 11, 12, 13, 16 3

0Dates: 25,4,9

4,9,259,25,44,25,9

Numeros: 0, 0, 022,22,220true

Page 23: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

generics.java

Page 24: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

ArrayListMethods

© A+ Computer Science - www.apluscompsci.com

Page 25: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayListfrequently used methods

Name Use

add(item) adds item to the end of the list

add(spot,item) adds item at spot – shifts items up->

set(spot,item) put item at spot z[spot]=item

get(spot) returns the item at spot return z[spot]

size() returns the # of items in the list

remove( int spot ) removes item at spot from the list

import java.util.ArrayList;

Page 26: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<String> vals;vals = new ArrayList<String>();vals.add("aplus");vals.add("rocks");vals.add(0, "comp");vals.add(1, "sci");out.println(vals);

OUTPUT

[comp, sci, aplus, rocks]

add( )

Page 27: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> nums;nums = new ArrayList<Integer>();nums.add(34);nums.add(0,99);nums.add(21);nums.add(1,7);nums.add(3);nums.add(11);out.println(nums);

OUTPUT

[99, 7, 34, 21, 3, 11]

add( )

Page 28: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

addone.javaaddtwo.java

Page 29: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.set(1,5);ray.add(4);ray.set(0,7);ray.add(53);out.println(ray);

OUTPUT

[7, 5, 4, 53]

set( )

Page 30: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.set(1,5);ray.add(4);ray.set(0,7);ray.add(53);out.println(ray.set(1,93) );

OUTPUT

5

set( )

Page 31: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(0, 11);ray.set(5,66);out.println(ray);

OUTPUT

Runtime exception

set( )

Page 32: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.add(12);ray.add(65);

out.println(ray.get(0));out.println(ray.get(1));out.println(ray.get(2));

OUTPUT231112

.get(spot) returns the reference stored at spot!

get( )

Page 33: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<Integer> ray;ray = new ArrayList<Integer>();ray.add(23);ray.add(11);ray.add(12);ray.add(65);

out.println(ray.get(3));out.println(ray.get(4));

OUTPUT65

Runtime exception

.get(spot) returns the reference stored at spot!

get( )

Page 34: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

set.javaget.java

Page 35: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

ProcessingLists with

Loops

© A+ Computer Science - www.apluscompsci.com

Page 36: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

for (int i=0; i<ray.size(); i++){ out.println(ray.get(i)); }

.size( ) returns the number of elements/items/spots/boxes or whatever you want to call them.

Standard For Loop

Page 37: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);

for( int i = 0; i < ray.size(); i++){ out.println( ray.get( i ) );}

OUTPUT

2311

Standard For Loop

Page 38: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

for ( int item : bunchOfNums ){ out.println( item ); }

The for each loop will access each reference in the list, starting with the first and ending with the last.

For Each Loop

Page 39: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

for ( Integer num : primeList ){ out.println( num ); }

The for each loop will access each reference in the list, starting with the first and ending with the last.

For Each Loop

Page 40: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(53);

for(int num : ray){ out.println(num);}

OUTPUT

231153

For Each Loop

Page 41: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

List<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(53);

for(Integer num : ray){ out.println(num);}

OUTPUT

231153

For Each Loop

Page 42: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

forloopone.javaforeachloopone.java

Page 43: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

RemovingItems

© A+ Computer Science - www.apluscompsci.com

Page 44: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("b");ray.remove(0);ray.add("c");ray.add("d");ray.remove(0);out.println(ray);

OUTPUT

[c, d]

remove( )

Page 45: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("b");out.println(ray.remove(0) );

OUTPUT

a

remove( )

Page 46: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

removeone.java

Page 47: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

BasicJava

Output

© A+ Computer Science - www.apluscompsci.com

Page 48: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

spot = size – 1while( spot is greater than

or equal to 0 ){ if ( this item is a match ) remove this item from the list subtract 1 from spot}

removing multiple values

Page 49: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

int spot = list.size() – 1;while( spot >= 0 ){ if ( list.get(spot).equals( value ) ) list.remove( spot ); spot = spot – 1;}

removing multiple values

Page 50: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

removeall.java

Page 51: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<String> ray;ray = new ArrayList<String>();

ray.add("a");ray.add("x");ray.clear();ray.add("t");ray.add("w");out.println(ray);

OUTPUT

[t, w]

clear( )

Page 52: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

clear.java

Page 53: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

CollectionsClass

© A+ Computer Science - www.apluscompsci.com

Page 54: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

Collectionsfrequently used methods

Name Use

sort(x) puts all items in x in ascending order

binarySearch(x,y) checks x for the location of y

fill(x,y) fills all spots in x with value y

rotate(x,y) shifts items in x left or right y locations

reverse(x) reverses the order of the items in x

import java.util.Collections;

Page 55: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(66);ray.add(53);Collections.sort(ray);out.println(ray);out.println(Collections.binarySearch(ray,677));out.println(Collections.binarySearch(ray,66));

OUTPUT[11, 23, 53, 66]-53

Collections

Page 56: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(53);out.println(ray);rotate(ray,2);out.println(ray);rotate(ray,2);reverse(ray);out.println(ray);

OUTPUT[23, 11, 53][11, 53, 23][11, 23, 53]

Collections

Page 57: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();ray.add(0);ray.add(0);ray.add(0);out.println(ray);

Collections.fill(ray,33);out.println(ray);

OUTPUT[0, 0, 0][33, 33, 33]

Collections

Page 58: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

binarysearch.javarotate.java

fill.java

Page 59: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

SearchMethods

© A+ Computer Science - www.apluscompsci.com

Page 60: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayListfrequently used methods

Name Use

contains(x) checks if the list contains x

indexOf(x) checks the list for the location of x

Page 61: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

ArrayList<Integer> ray;ray = new ArrayList<Integer>();

ray.add(23);ray.add(11);ray.add(66);ray.add(53);

out.println(ray);out.println(ray.indexOf(21));out.println(ray.indexOf(66));

out.println(ray);out.println(ray.contains(21));out.println(ray.contains(66));

OUTPUT[23, 11, 66, 53]-12[23, 11, 66, 53]falsetrue

Page 62: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

search.java

Page 63: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science - www.apluscompsci.com

Work on Programs!

Crank Some Code!

Page 64: A+ Computer Science LISTS · Primitive variables carry their values with them like color coded sticky notes int age = 18; Every time I declare a variable, a new sticky note is created

© A+ Computer Science -

www.apluscompsci.com

A+ Computer Science

LISTS