Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
© A+ Computer Science -
www.apluscompsci.com
A+ Computer Science
LISTS
What isa List?
© A+ Computer Science - www.apluscompsci.com
Problems with Arrays:What are the limitations of Arrays?
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
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!
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.
What isa List?
© A+ Computer Science - www.apluscompsci.com
Stacey Got a Little Ahead of Himself.Let’s do a quick preview lesson on:
REFERENCE VARIABLES(Sometimes called “Pointers”)
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
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
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
© 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
© 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
© 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
© A+ Computer Science - www.apluscompsci.com
aplus
nothing
null
null
ArrayList aplus;
aplus is a reference to an ArrayList.
ArrayList - Dynamic memory
Generics
© A+ Computer Science - www.apluscompsci.com
© 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.”
© 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
© 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
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
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)
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
generics.java
ArrayListMethods
© A+ Computer Science - www.apluscompsci.com
© 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;
© 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( )
© 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( )
addone.javaaddtwo.java
© 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( )
© 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( )
© 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( )
© 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( )
© 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( )
set.javaget.java
ProcessingLists with
Loops
© A+ Computer Science - www.apluscompsci.com
© 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
© 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
© 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
© 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
© 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
© 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
forloopone.javaforeachloopone.java
RemovingItems
© A+ Computer Science - www.apluscompsci.com
© 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( )
© 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( )
removeone.java
BasicJava
Output
© A+ Computer Science - www.apluscompsci.com
© 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
© 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
removeall.java
© 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( )
clear.java
CollectionsClass
© A+ Computer Science - www.apluscompsci.com
© 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;
© 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
© 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
© 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
binarysearch.javarotate.java
fill.java
SearchMethods
© A+ Computer Science - www.apluscompsci.com
© 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
© 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
search.java
© A+ Computer Science - www.apluscompsci.com
Work on Programs!
Crank Some Code!
© A+ Computer Science -
www.apluscompsci.com
A+ Computer Science
LISTS