crea ng and accessing arrays crea ng and accessing arrays
play

Crea%ng and Accessing Arrays Crea%ng and Accessing Arrays - PDF document

Array Basics: Outline Crea%ng and Accessing Arrays More Arrays Array Details (Savitch, Chapter 7) The Instance Variable length TOPICS More About Array Indices


  1. Array ¡Basics: ¡Outline ¡ • Crea%ng ¡and ¡Accessing ¡Arrays ¡ More Arrays • Array ¡Details ¡ (Savitch, Chapter 7) • The ¡Instance ¡Variable ¡length ¡ TOPICS • More ¡About ¡Array ¡Indices ¡ • Array Basics • Analyzing ¡Arrays ¡ • Arrays in Classes and Methods • Programming with Arrays • Searching and Sorting Arrays • Multi-Dimensional Arrays • Static Variables and Constants CS160 - Spring Semester 2016 2 Crea%ng ¡and ¡Accessing ¡Arrays ¡ Crea%ng ¡and ¡Accessing ¡Arrays ¡ • An ¡array ¡is ¡a ¡special ¡kind ¡of ¡object ¡ • Figure ¡7.1 ¡ ¡A ¡common ¡way ¡to ¡visualize ¡an ¡ • Think ¡of ¡as ¡collec%on ¡of ¡variables ¡of ¡same ¡type ¡ array ¡ • Crea%ng ¡an ¡array ¡with ¡7 ¡variables ¡of ¡type ¡double ¡ • To ¡access ¡an ¡element ¡use ¡ – The ¡name ¡of ¡the ¡array ¡ – An ¡index ¡number ¡enclosed ¡in ¡braces ¡ • Note ¡ ¡ class ArrayOfTemperatures • Array ¡indices ¡begin ¡at ¡zero ¡ CS160 - Spring Semester 2016 3 CS160 - Spring Semester 2016 4 1

  2. Crea%ng ¡and ¡Accessing ¡Arrays ¡ Array ¡Details ¡ • Syntax ¡for ¡declaring ¡an ¡array ¡with ¡ new Sample • The ¡number ¡of ¡elements ¡in ¡an ¡array ¡is ¡its ¡ screen output length ¡ • The ¡type ¡of ¡the ¡array ¡elements ¡is ¡the ¡array's ¡ base ¡type ¡ CS160 - Spring Semester 2016 5 CS160 - Spring Semester 2016 6 Square ¡Brackets ¡with ¡Arrays ¡ Array ¡Details ¡ • With ¡a ¡data ¡type ¡when ¡declaring ¡an ¡array: ¡ • Figure ¡7.2 ¡Array ¡terminology ¡ ¡ int []pressure; int pressure[]; • To ¡enclose ¡an ¡integer ¡expression ¡to ¡declare ¡ the ¡length ¡of ¡the ¡array: ¡ ¡ pressure = new int[100]; • To ¡access ¡the ¡array ¡with ¡an ¡index ¡value: ¡ ¡ pressure[3] = keyboard.nextInt(); CS160 - Spring Semester 2016 7 CS160 - Spring Semester 2016 8 2

  3. The ¡Instance ¡Variable ¡ length The ¡Instance ¡Variable ¡ length ¡ • As ¡an ¡object ¡an ¡array ¡has ¡only ¡one ¡public ¡ instance ¡variable: ¡ – Variable ¡ length – Contains ¡number ¡of ¡elements ¡in ¡the ¡array ¡ Sample – It ¡is ¡final, ¡value ¡cannot ¡be ¡changed ¡ screen output • Note ¡ ¡ class ArrayOfTemperatures2 CS160 - Spring Semester 2016 9 CS160 - Spring Semester 2016 10 More ¡About ¡Array ¡Indices ¡ Ini%alizing ¡Arrays ¡ • Possible ¡to ¡ini%alize ¡at ¡declara%on ¡%me ¡ • Index ¡of ¡first ¡array ¡element ¡is ¡0 ¡ • Last ¡valid ¡Index ¡is ¡ arrayName.length – 1 • Array ¡indices ¡must ¡be ¡within ¡bounds ¡to ¡be ¡ valid: ¡ • Also ¡may ¡use ¡normal ¡assignment ¡statements ¡ – When ¡program ¡tries ¡to ¡access ¡outside ¡bounds, ¡run ¡ – One ¡at ¡a ¡%me ¡ %me ¡excep%on ¡occurs ¡ – In ¡a ¡loop ¡ • Get ¡used ¡to ¡using ¡index ¡0 ¡ CS160 - Spring Semester 2016 11 CS160 - Spring Semester 2016 12 3

  4. Arrays ¡in ¡Classes ¡and ¡Methods: ¡ Indexed ¡Variables ¡as ¡Method ¡ Outline ¡ Arguments ¡ • Indexed ¡variable ¡of ¡an ¡array ¡ • Indexed ¡Variables ¡as ¡Method ¡Arguments ¡ – Example ¡… ¡ a[i] • En%re ¡Arrays ¡as ¡Arguments ¡to ¡a ¡Method ¡ – Can ¡be ¡used ¡anywhere ¡variable ¡of ¡array ¡base ¡type ¡ • Arguments ¡for ¡the ¡Method ¡main ¡ can ¡be ¡used ¡ • Array ¡Assignment ¡and ¡Equality ¡ • View ¡class ¡ArgumentDemo ¡using ¡indexed ¡ • Methods ¡that ¡Return ¡Arrays ¡ variable ¡as ¡an ¡argument ¡ CS160 - Spring Semester 2016 13 CS160 - Spring Semester 2016 14 En%re ¡Arrays ¡as ¡Arguments ¡ En%re ¡Arrays ¡as ¡Arguments ¡ • Declara%on ¡of ¡array ¡parameter ¡similar ¡to ¡how ¡ • Array ¡parameter ¡in ¡a ¡method ¡heading ¡does ¡ an ¡array ¡is ¡declared ¡ not ¡specify ¡the ¡length ¡ – An ¡array ¡of ¡any ¡length ¡can ¡be ¡passed ¡to ¡the ¡ • Example: ¡ method ¡ – Inside ¡the ¡method, ¡elements ¡of ¡the ¡array ¡can ¡be ¡ changed ¡ • When ¡you ¡pass ¡the ¡en%re ¡array, ¡do ¡not ¡use ¡ square ¡brackets ¡in ¡the ¡actual ¡parameter ¡ CS160 - Spring Semester 2016 15 CS160 - Spring Semester 2016 16 4

  5. Arguments ¡for ¡Method ¡main ¡ Array ¡Assignment ¡and ¡Equality ¡ • Recall ¡heading ¡of ¡method ¡ main ¡ • Arrays ¡are ¡objects ¡ public static void main (String[] args) – Assignment ¡and ¡equality ¡operators ¡behave ¡ • This ¡declares ¡an ¡array ¡ ¡ (misbehave) ¡as ¡specified ¡in ¡previous ¡chapter ¡ • Variable ¡for ¡the ¡array ¡object ¡contains ¡memory ¡ – Formal ¡parameter ¡named ¡ args address ¡of ¡the ¡object ¡ – Its ¡base ¡type ¡is ¡ String – Assignment ¡operator ¡ = copies ¡this ¡address ¡ • Thus ¡possible ¡to ¡pass ¡to ¡the ¡run ¡of ¡a ¡program ¡ mul%ple ¡strings ¡ – Equality ¡operator ¡ == ¡ ¡tests ¡whether ¡two ¡arrays ¡ are ¡stored ¡in ¡same ¡place ¡in ¡memory ¡ – These ¡can ¡then ¡be ¡used ¡by ¡the ¡program ¡ CS160 - Spring Semester 2016 17 CS160 - Spring Semester 2016 18 Array ¡Assignment ¡and ¡Equality ¡ Array ¡Assignment ¡and ¡Equality ¡ • Note ¡results ¡of ¡ == • Two ¡kinds ¡of ¡equality ¡ • Note ¡defini%on ¡and ¡use ¡of ¡method ¡ equals • View ¡ ¡ class TestEquals – Receives ¡two ¡array ¡parameters ¡ – Checks ¡length ¡and ¡each ¡individual ¡pair ¡of ¡array ¡ Sample screen elements ¡ output • Remember ¡array ¡types ¡are ¡reference ¡types ¡ CS160 - Spring Semester 2016 19 CS160 - Spring Semester 2016 20 5

  6. Programming ¡with ¡Arrays ¡ Methods ¡that ¡Return ¡Arrays ¡ and ¡Classes: ¡Outline ¡ • A ¡Java ¡method ¡may ¡return ¡an ¡array ¡ • View ¡ class ReturnArrayDemo • Programming ¡Example: ¡A ¡Specialized ¡List ¡Class ¡ • Note ¡defini%on ¡of ¡return ¡type ¡as ¡an ¡array ¡ • Par%ally ¡Filled ¡Arrays ¡ • To ¡return ¡the ¡array ¡value ¡ – Declare ¡a ¡local ¡array ¡ – Use ¡that ¡iden%fier ¡in ¡the ¡ return ¡statement ¡ CS160 - Spring Semester 2016 21 CS160 - Spring Semester 2016 22 Programming ¡Example ¡ Programming ¡Example ¡ • A ¡specialized ¡List ¡class ¡ • ¡View ¡ class ListDemo – Objects ¡can ¡be ¡used ¡for ¡keeping ¡lists ¡of ¡items ¡ • Note ¡declara%on ¡of ¡the ¡list ¡object ¡ • Methods ¡include ¡ • Note ¡method ¡calls ¡ – Capability ¡to ¡add ¡items ¡to ¡the ¡list ¡ – Also ¡delete ¡en%re ¡list, ¡start ¡with ¡blank ¡list ¡ – But ¡no ¡method ¡to ¡modify ¡or ¡delete ¡list ¡item ¡ • Maximum ¡number ¡of ¡items ¡can ¡be ¡specified ¡ CS160 - Spring Semester 2016 23 CS160 - Spring Semester 2016 24 6

  7. Programming ¡Example ¡ Programming ¡Example ¡ • Now ¡view ¡array ¡wrapped ¡in ¡a ¡class ¡to ¡ represent ¡a ¡list, ¡ class OneWayNoRepeatsList • Notable ¡code ¡elements ¡ Sample screen – Declara%on ¡of ¡private ¡array ¡ output – Method ¡to ¡find ¡n th ¡ list ¡item ¡ – Method ¡to ¡check ¡if ¡item ¡is ¡on ¡the ¡list ¡or ¡not ¡ CS160 - Spring Semester 2016 25 CS160 - Spring Semester 2016 26 Par%ally ¡Filled ¡Arrays ¡ Par%ally ¡Filled ¡Arrays ¡ • Array ¡size ¡specified ¡at ¡defini%on ¡ • Figure ¡7.4 ¡ ¡A ¡par%ally ¡filled ¡array ¡ • Not ¡all ¡elements ¡of ¡the ¡array ¡might ¡receive ¡ values ¡ – This ¡is ¡termed ¡a ¡ par$ally ¡filled ¡array ¡ • Programmer ¡must ¡keep ¡track ¡of ¡how ¡much ¡of ¡ array ¡is ¡used ¡ CS160 - Spring Semester 2016 27 CS160 - Spring Semester 2016 28 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend