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.

Slides:



Advertisements
Similar presentations
Two Dimensional Arrays and ArrayList
Advertisements

Arrays.
One Dimensional Arrays
Chapter 10 Introduction to Arrays
Arrays.
Chapter 7 – Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
Building Java Programs
Chapter 9 Introduction to Arrays
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Arrays.
COMP More About Arrays Yi Hong June 05, 2015.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Lesson 8: Introduction To Arrays
Lecture 5 of Computer Science II
Chapter 8 – Arrays and Array Lists
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Can store many of the same kind of data together
Chapter 8 Slides from GaddisText
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Can store many of the same kind of data together
Building Java Programs
Can store many of the same kind of data together
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays.
Presentation transcript:

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

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

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

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};

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

Ch – 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

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

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

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

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]; ….} ….}

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

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…}

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.

Ch – 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 You create: Document Index and IndexEntry – info on pages

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

12.4 Case study: chomp Case study: chomp P P

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