Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Arrays.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
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.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 7 – Arrays and Array Lists.
Components of Computers CSH. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Schematic Diagram of a Computer.
Main Menu Salas, Hille, Etgen Calculus: One and Several Variables Copyright 2007 © John Wiley & Sons, Inc. All rights reserved. The Indeterminate Form.
Applied Calculus, 3/E by Deborah Hughes-Hallet Copyright 2006 by John Wiley & Sons. All rights reserved. Section 9.4 Computing Partial Derivatives Algebraically.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Chapter Goals To collect elements using arrays and array lists
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
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 =
Static. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static method does not operate on an object double dY.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao.
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.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 6 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Algebra and Trigonometry by Cynthia Y. Young, © 2007 John Wiley and Sons. All rights reserved. Solving Systems of Equations.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Decisions Bush decision making.
Copyright © 2013 by John Wiley & Sons. All rights reserved. ARRAYS and ARRAYLISTS CHAPTER Slides by Donald W. Smith TechNeTrain.com 6 Final Draft 10/30/2011.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
Slides by Evan Gallagher
Slides by Evan Gallagher
A final variable is a constant
Chapter 7 – Arrays and Array Lists
Chapter 8 – Arrays and Array Lists
Chapter 7 – Arrays.
Chapter 4 – Fundamental Data Types
7.6 Common Array Algorithm: Filling
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter Three - Implementing Classes
Copying ArrayLists Copied Section of
Slides by Donald W. Smith
Use a variable to store a value that you want to use at a later time
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
Presentation transcript:

Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming convention: final int VALUES_LENGTH = 100; double[] values = new double[VALUES_LENGTH]; int valuesSize = 0; Update valuesSize as array is filled: values[valuesSize] = x; valuesSize++; 7.5 Partially Filled Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Partially Filled Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Example: Read numbers into a partially filled array: int valuesSize = 0; Scanner in = new Scanner(System.in); while (in.hasNextDouble()) { if (valuesSize < values.length) { values[valuesSize] = in.nextDouble(); valuesSize++; } } To process the gathered array elements, use the companion variable, not the array length: for (int i = 0; i < valuesSize; i++) { System.out.println(values[i]); } Partially Filled Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Write a loop to print the elements of the partially filled array values in reverse order, starting with the last element. Answer: for (int i = valuesSize - 1; i >= 0; i--) System.out.println(values[i]); Self Check 7.9

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How do you remove the last element of the partially filled array values ? Answer: valuesSize--; Self Check 7.10

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Why would a programmer use a partially filled array of numbers instead of an array list? Answer: You need to use wrapper objects in an ArrayList, which is less efficient. Self Check 7.11