18
AP Comp Sci A AP Comp Sci A Chapter 12 - Arrays Chapter 12 - Arrays

AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Embed Size (px)

Citation preview

Page 1: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

AP Comp Sci AAP Comp Sci A

Chapter 12 - ArraysChapter 12 - Arrays

Page 2: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Goals:Goals: Declare and create arraysDeclare and create arrays Access elements in arraysAccess elements in arrays Pass arrays as parametersPass arrays as parameters Use the ArrayList classUse the ArrayList class Traverse arraysTraverse arrays Make 2 dimensional arraysMake 2 dimensional arrays

Page 3: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Array – several consecutive blocks of Array – several consecutive blocks of memorymemory

Good to use for groups of numbersGood to use for groups of numbers Ex) test scoresEx) test scores int[] scoresint[] scores; ; //declare array of type

int scores = new int[5];scores = new int[5]; //create new

array with 5 elements

Page 4: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

index – individual locationindex – individual location int[] scores = new int[5]; //each int[] scores = new int[5]; //each

element is initialized to null valueelement is initialized to null value score[3] = 85;score[3] = 85; Where does it go?Where does it go?

85

Page 5: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Declare and initialize array without Declare and initialize array without specifying size:specifying size:

int[] scores = {50,12,67,85,95};int[] scores = {50,12,67,85,95};

9512 67 8550

Page 6: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Note: once an array is declared and Note: once an array is declared and initialized, you CANNOT change it’s initialized, you CANNOT change it’s sizesize

You can find an array’s length:You can find an array’s length: Int x = scores.length; (length is not Int x = scores.length; (length is not

a method!)a method!)

Page 7: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

12.3 – Lab12.3 – Lab Complete the fortune tellerComplete the fortune teller fortuneteller.javafortuneteller.java Needs a string array with some sayingsNeeds a string array with some sayings Under actionperformed:Under actionperformed: Needs an integer that has a random Needs an integer that has a random

number for the range of the arraynumber for the range of the array In display.setText – replace … with the In display.setText – replace … with the

arrayarray

Page 8: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Array’s can be problems if they get filled upArray’s can be problems if they get filled up ArrayList classArrayList class - keeps track of its capacity and size- keeps track of its capacity and size - automatically increases in size when needed- automatically increases in size when needed import java.util.*;import java.util.*; private ArrayList<String> scores; private ArrayList<String> scores; //only holds //only holds

objects. objects. you can use primitive data, but it you can use primitive data, but it converts it into an objectconverts it into an object

Scores = new ArrayList<String>; Scores = new ArrayList<String>; //ArrayList //ArrayList holds objectsholds objects

Page 9: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Check out page 331 for ArrayList methodsCheck out page 331 for ArrayList methods In class:In class: Create an ArrayList that holds the names Create an ArrayList that holds the names

of 4 Lord of the Rings charactersof 4 Lord of the Rings characters Use methods to: add Clint Eastwood to Use methods to: add Clint Eastwood to

the 2nd position; the 2nd position; add Lebron James to the add Lebron James to the end;end; check if the list contains Frodo check if the list contains Frodo Baggins; Baggins; returns index of Gandalf the returns index of Gandalf the Grey;Grey; returns and prints String returns and prints String representation of this list representation of this list

Page 10: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Accessing elementsAccessing elements For large arrays, it is impractical to call For large arrays, it is impractical to call

each element with a statementeach element with a statement Use for loopUse for loop int sum = 0;int sum = 0; for(int i = 0; i <1000, ;i++)for(int i = 0; i <1000, ;i++) sum+= a[i];sum+= a[i]; //not useful if we don’t know size of array //not useful if we don’t know size of array

in advancein advance

Page 11: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Traversal – procedure where every Traversal – procedure where every element in array is processedelement in array is processed

When array size is unknownWhen array size is unknown String[] names = new String[] names = new

String[numGuests];String[numGuests]; For (int I = 0; I <names.length; i++)For (int I = 0; I <names.length; i++) { String str = name[i];{ String str = name[i]; …….}.}

Page 12: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

In traversing a set of 10 ints, how In traversing a set of 10 ints, how would we find the largest element?would we find the largest element?

Write code that would accomplish Write code that would accomplish the following:the following:

Keep track of the largest valueKeep track of the largest value Keep track of that indexKeep track of that index

Page 13: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Inserting:Inserting: Prevent error for inserting into full arrayPrevent error for inserting into full array Final int maxCount = 5000;Final int maxCount = 5000; String[] dictionary = new String[] dictionary = new

String[maxCount];String[maxCount];

Int count = 0;Int count = 0;

If (count < MaxCount)If (count < MaxCount)

{ … do insert…}{ … do insert…}

Page 14: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

When inserting, you may have to insert in When inserting, you may have to insert in the middle of an arraythe middle of an array

This means you will have to shift data downThis means you will have to shift data down Shift from last element, or you may erase Shift from last element, or you may erase

datadata *show on board, or see page 339*show on board, or see page 339 In class : create an array of size 12. fill the In class : create an array of size 12. fill the

first 10 indices with then numbers 0-9. first 10 indices with then numbers 0-9. Insert the number 77 between 4 and 5.Insert the number 77 between 4 and 5.

Page 15: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

12.9 – lab12.9 – lab Program – reads a text file, makes a Program – reads a text file, makes a

new file that puts all words in new file that puts all words in alphabetical order, followed by what alphabetical order, followed by what line the word appear(s)line the word appear(s)

You create: Document Index and You create: Document Index and IndexEntry – info on pages 342-343IndexEntry – info on pages 342-343

Page 16: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

2d arrays2d arrays Declare with 2 bracketsDeclare with 2 brackets Int [] [] a; Int [] [] a; 1. int[] [] a = new int[3] [2]; 1. int[] [] a = new int[3] [2]; //3 rows //3 rows

by 2 columnby 2 column 2. int [] [] a = {1, 2, 3},{4,5,6}; 2. int [] [] a = {1, 2, 3},{4,5,6};

//declare and initialize 2 by 3//declare and initialize 2 by 3 Traversal – use 2 nested for loopsTraversal – use 2 nested for loops

Page 17: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

12.412.4

Case study: chompCase study: chomp P346-351P346-351

Page 18: AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in

Ch 12Ch 12

Exercise setExercise set #1,2,3,6,11,19, 28, 29, 30#1,2,3,6,11,19, 28, 29, 30