Chapter 7: Arrays
CS 121
Department of Computer Science College of Engineering Boise State University
April 9, 2015
Chapter 7: Arrays CS 121 1 / 41
Chapter 7: Arrays CS 121 Department of Computer Science College of - - PowerPoint PPT Presentation
Chapter 7: Arrays CS 121 Department of Computer Science College of Engineering Boise State University April 9, 2015 Chapter 7: Arrays CS 121 1 / 41 Topics Array declaration and use Go to part 0 Bounds checking Go to part 1
Chapter 7: Arrays CS 121 1 / 41
Go to part 0
Go to part 1
Go to part 2
Go to part 3
Go to part 4
Go to part 5
Go to part 6 Chapter 7: Arrays CS 121 2 / 41
Chapter 7: Arrays CS 121 3 / 41
◮ For example, our scores array has 5 elements that are indexed
Chapter 7: Arrays CS 121 4 / 41
Chapter 7: Arrays CS 121 5 / 41
Chapter 7: Arrays CS 121 6 / 41
Chapter 7: Arrays CS 121 7 / 41
Chapter 7: Arrays CS 121 8 / 41
/** * BasicArray.java - Demonstrates basic array declaration and use. * @author Java Foundations */ public class BasicArray { /** * Creates an array , fills it with various integer values , * modifies
prints them out. */ public static void main(String [] args) { final int LIMIT = 15, MULTIPLE = 10; int[] list = new int[LIMIT ]; // Initialize the array values for (int index = 0; index < LIMIT; index ++) list[index] = index * MULTIPLE; list [5] = 999; // change one array value // Print the array values for (int value: list) System.out.print(value + " "); } }
Chapter 7: Arrays CS 121 9 / 41
Chapter 7: Arrays CS 121 10 / 41
Chapter 7: Arrays CS 121 11 / 41
Chapter 7: Arrays CS 121 12 / 41
Chapter 7: Arrays CS 121 13 / 41
◮ Reads a list of numbers from a user and prints it in the
◮ Reads a sentence and prints the counts of lowercase and
Chapter 7: Arrays CS 121 14 / 41
Chapter 7: Arrays CS 121 15 / 41
Chapter 7: Arrays CS 121 16 / 41
Chapter 7: Arrays CS 121 17 / 41
Chapter 7: Arrays CS 121 18 / 41
Chapter 7: Arrays CS 121 19 / 41
Chapter 7: Arrays CS 121 20 / 41
◮ Minimum space: We could grow the array by one elements so
◮ Minimum time: Grow the array to the maximum size we will
◮ Heuristic: A good heuristic is to double the size so we don’t
Chapter 7: Arrays CS 121 21 / 41
Chapter 7: Arrays CS 121 22 / 41
Chapter 7: Arrays CS 121 23 / 41
Chapter 7: Arrays CS 121 24 / 41
Chapter 7: Arrays CS 121 25 / 41
Chapter 7: Arrays CS 121 26 / 41
Chapter 7: Arrays CS 121 27 / 41
Chapter 7: Arrays CS 121 28 / 41
◮ the number of arguments ◮ the type of arguments ◮ the values are within a specific range
Chapter 7: Arrays CS 121 29 / 41
Chapter 7: Arrays CS 121 30 / 41
Chapter 7: Arrays CS 121 31 / 41
Chapter 7: Arrays CS 121 32 / 41
Chapter 7: Arrays CS 121 33 / 41
Chapter 7: Arrays CS 121 34 / 41
Chapter 7: Arrays CS 121 35 / 41
Chapter 7: Arrays CS 121 36 / 41
Chapter 7: Arrays CS 121 37 / 41
◮ A spreadsheet is a 2-dimensional array. The tabs would make
◮ Simulations of liquids, solids, space etc. ◮ Modeling in science and engineering.
Chapter 7: Arrays CS 121 38 / 41
◮ Consider a 3-dim array to represent a universe that has a 100 galaxies.
Star [][][] myUniverse = new Star [100][1000][10]; public class Star { ... }
◮ Here is a different design that avoids the multidimensional array.
Galaxy [] myUniverse = new Galaxy [100]; public class Galaxy { private Cluster [] myClusters = new Cluster [1000]; // other related instance variables } public class Cluster { private Star [] myStars = new Star [10]; // other related instance variables } public class Star { ... }
Chapter 7: Arrays CS 121 39 / 41
◮ add(String element): adds an element to the end of the array
◮ add(String element, int index): adds an element at the
◮ remove(int index): removes an element at the indexth
◮ contains(String s): returns true if the array list contains the
Chapter 7: Arrays CS 121 40 / 41
◮ Exercises: EX 7.1, 7.4 (e), 7.5, 7.8. ◮ Projects: PP 7.1, 7.2, 7.5.
Chapter 7: Arrays CS 121 41 / 41