Arrays - 1.4 Arrays Introduction to Programming in Java An...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2008 * * 2 A Foundation for Programming objects functions and modules graphics, sound, and image I/O arrays conditionals and loops Math text I/O assignment statements primitive data types store and manipulate huge quantities of data any program you might want to write 3 Arrays This lecture. Store and manipulate huge quantities of data. Array. Indexed sequence of values of the same type. Examples. ! 52 playing cards in a deck. ! 5 thousand undergrads at Princeton. ! 1 million characters in a book. ! 10 million audio samples in an MP3 file. ! 4 billion nucleotides in a DNA strand. ! 73 billion Google queries per year. ! 50 trillion cells in the human body. ! 6.02 ! 10 23 particles in a mole. wayne 0 doug 1 rs 2 maia 3 mona 4 cbienia 5 wkj 6 mkc 7 index value 4 Many Variables of the Same Type Goal. 10 variables of the same type. // tedious and error-prone double a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 , a9 ; a0 = 0.0 ; a1 = 0.0 ; a2 = 0.0 ; a3 = 0.0 ; a4 = 0.0 ; a5 = 0.0 ; a6 = 0.0 ; a7 = 0.0 ; a9 = 0.0 ; a9 = 0.0 ; double x = a4 + a8 ; 5 Arrays in Java Java has special language support for arrays. ! To make an array: declare, create, and initialize it. ! To access element i of array named a , use a[i] . ! Array indices start at . int N = 10 ; double a ; // declare the array a = new double [ N ]; // create the array for ( int i = ; i < N ; i ++) // initialize the array a [ i ] = 0.0 ; // all to 0.0 6 Arrays in Java Java has special language support for arrays. ! To make an array: declare, create, and initialize it. ! To access element i of array named a , use a[i] . ! Array indices start at . Compact alternative. ! Declare, create, and initialize in one statement. ! Default initialization: all numbers automatically set to zero. int N = 10 ; double a ; // declare the array a = new double [ N ]; // create the array for ( int i = ; i < N ; i ++) // initialize the array a [ i ] = 0.0 ; // all to 0.0 int N = 10 ; double a = new double [ N ]; // declare, create, init 7 Vector Dot Product Dot product. Given two vectors x and y of length N , their dot product is the sum of the products of their corresponding components. double x = { 0.3 , 0.6 , 0.1 } ; double y = { 0.5 , 0.1 , 0.4 } ; double sum = 0.0 ; for ( int i = ; i < N ; i ++) { sum += x [ i ]* y [ i ]; } 8 Array Processing Code 5 Arrays in Java Java has special language support for arrays. ! To make an array: declare, create, and initialize it. ! To access element i of array named a , use a[i] . ! Array indices start at . int N = 10 ; double a ; // declare the array a = new double [ N ]; // create the array for ( int i = ; i < N ; i ++) // initialize the array a [ i ] = 0.0 ; // all to 0.0 6 Arrays in Java Java has special language support for arrays....
View Full Document

This note was uploaded on 09/22/2008 for the course COS 126 taught by Professor Douglasw.clark during the Fall '08 term at Princeton.

Page1 / 9

Arrays - 1.4 Arrays Introduction to Programming in Java An...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online