Download presentation
Presentation is loading. Please wait.
Published byJohnathan Wells Modified over 9 years ago
1
AP Comp Sci A Chapter 12 - Arrays
2
Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in arrays Pass arrays as parameters Pass arrays as parameters Use the ArrayList class Use the ArrayList class Traverse arrays Traverse arrays Make 2 dimensional arrays Make 2 dimensional arrays
3
Ch 12 Array – several consecutive blocks of memory Array – several consecutive blocks of memory Good to use for groups of numbers Good to use for groups of numbers Ex) test scores Ex) test scores int[] scores; int[] scores; //declare array of type int scores = new int[5]; scores = new int[5]; //create new array with 5 elements
4
Ch 12 index – individual location index – individual location int[] scores = new int[5]; //each element is initialized to null value int[] scores = new int[5]; //each element is initialized to null value score[3] = 85; score[3] = 85; Where does it go? Where does it go? 85
5
Ch 12 Declare and initialize array without specifying size: Declare and initialize array without specifying size: int[] scores = {50,12,67,85,95}; int[] scores = {50,12,67,85,95}; 9512 67 85 50
6
Ch 12 Note: once an array is declared and initialized, you CANNOT change it’s size Note: once an array is declared and initialized, you CANNOT change it’s size You can find an array’s length: You can find an array’s length: Int x = scores.length; (length is not a method!) Int x = scores.length; (length is not a method!)
7
Ch 12 12.3 – Lab 12.3 – Lab Complete the fortune teller Complete the fortune teller fortuneteller.java fortuneteller.java Needs a string array with some sayings Needs a string array with some sayings Under actionperformed: Under actionperformed: Needs an integer that has a random number for the range of the array Needs an integer that has a random number for the range of the array In display.setText – replace … with the array In display.setText – replace … with the array
8
Ch 12 Array’s can be problems if they get filled up Array’s can be problems if they get filled up ArrayList class ArrayList 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 scores; //only holds objects. you can use primitive data, but it converts it into an object private ArrayList scores; //only holds objects. you can use primitive data, but it converts it into an object Scores = new ArrayList ; //ArrayList holds objects Scores = new ArrayList ; //ArrayList holds objects
9
Ch 12 Check out page 331 for ArrayList methods Check out page 331 for ArrayList methods In class: In class: Create an ArrayList that holds the names of 4 Lord of the Rings characters Create an ArrayList that holds the names of 4 Lord of the Rings characters Use methods to: add Clint Eastwood to the 2nd position; add Lebron James to the end; check if the list contains Frodo Baggins; returns index of Gandalf the Grey; returns and prints String representation of this list Use methods to: add Clint Eastwood to the 2nd position; add Lebron James to the end; check if the list contains Frodo Baggins; returns index of Gandalf the Grey; returns and prints String representation of this list
10
Ch 12 Accessing elements Accessing elements For large arrays, it is impractical to call each element with a statement For large arrays, it is impractical to call each element with a statement Use for loop Use 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 in advance //not useful if we don’t know size of array in advance
11
Ch 12 Traversal – procedure where every element in array is processed Traversal – procedure where every element in array is processed When array size is unknown When array size is unknown String[] names = new String[numGuests]; String[] names = new 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]; ….} ….}
12
Ch 12 In traversing a set of 10 ints, how would we find the largest element? In traversing a set of 10 ints, how would we find the largest element? Write code that would accomplish the following: Write code that would accomplish the following: Keep track of the largest value Keep track of the largest value Keep track of that index Keep track of that index
13
Ch 12 Inserting: Inserting: Prevent error for inserting into full array Prevent error for inserting into full array Final int maxCount = 5000; Final int maxCount = 5000; String[] dictionary = new String[maxCount]; String[] dictionary = new String[maxCount]; Int count = 0; If (count < MaxCount) { … do insert…}
14
Ch 12 When inserting, you may have to insert in the middle of an array When inserting, you may have to insert in the middle of an array This means you will have to shift data down This means you will have to shift data down Shift from last element, or you may erase data Shift from last element, or you may erase data *show on board, or see page 339 *show on board, or see page 339 In class : create an array of size 12. fill the first 10 indices with then numbers 0-9. Insert the number 77 between 4 and 5. In class : create an array of size 12. fill the first 10 indices with then numbers 0-9. Insert the number 77 between 4 and 5.
15
Ch 12 12.9 – lab 12.9 – lab Program – reads a text file, makes a new file that puts all words in alphabetical order, followed by what line the word appear(s) Program – reads a text file, makes a new file that puts all words in alphabetical order, followed by what line the word appear(s) You create: Document Index and IndexEntry – info on pages 342-343 You create: Document Index and IndexEntry – info on pages 342-343
16
Ch 12 2d arrays 2d arrays Declare with 2 brackets Declare with 2 brackets Int [] [] a; Int [] [] a; 1. int[] [] a = new int[3] [2]; //3 rows by 2 column 1. int[] [] a = new int[3] [2]; //3 rows by 2 column 2. int [] [] a = {1, 2, 3},{4,5,6}; //declare and initialize 2 by 3 2. int [] [] a = {1, 2, 3},{4,5,6}; //declare and initialize 2 by 3 Traversal – use 2 nested for loops Traversal – use 2 nested for loops
17
12.4 Case study: chomp Case study: chomp P346-351 P346-351
18
Ch 12 Exercise set Exercise set #1,2,3,6,11,19, 28, 29, 30 #1,2,3,6,11,19, 28, 29, 30
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.