Question? Leave a message!




Arrays in Java

Arrays in Java
Dr.AlexanderTyler Profile Pic
Dr.AlexanderTyler,India,Teacher
Published Date:21-07-2017
Website URL
Comment
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33 PM A Foundation for Programming any program you might want to write objects functions and modules graphics, sound, and image I/O store and manipulate arrays huge quantities of data conditionals and loops Math text I/O primitive data types assignment statements 2 Arrays This lecture. Store and manipulate huge quantities of data. Array. Indexed sequence of values of the same type. index value Examples. 0 wayne   52 playing cards in a deck. 1 rs   5 thousand undergrads at Princeton. 2 doug   1 million characters in a book. 3 dgabai   10 million audio samples in an MP3 file. 4 maia   4 billion nucleotides in a DNA strand. 5 llp   73 billion Google queries per year. 6 funk   50 trillion cells in the human body. 7 blei 23   6.02 10 particles in a mole. 3 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; a8 = 0.0; a9 = 0.0; … a4 = 3.0; … a4 = 8.0; … double x = a4 + a8; 4 Many Variables of the Same Type Goal. 10 variables of the same type. // easy alternative double a = new double10; … a4 = 3.0; declares, creates, and initializes … stay tuned for details a8 = 8.0; … double x = a4 + a8; 5 Many Variables of the Same Type Goal. 1 million variables of the same type. // scales to handle large arrays double a = new double1000000; … a123456 = 3.0; declares, creates, and initializes … stay tuned for details a987654 = 8.0; … double x = a123456 + a987654; 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 ai.   Array indices start at 0. int N = 10; // size of array double a; // declare the array a = new doubleN; // create the array for (int i = 0; i N; i++) // initialize the array ai = 0.0; // all to 0.0 Compact alternative.   Declare, create, and initialize in one statement.   Default initialization: all numbers automatically set to zero. int N = 10; // size of array double a = new doubleN; // declare, create, init 8 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 ; int N = x.length; double sum = 0.0; for (int i = 0; i N; i++) sum = sum + xiyi; 9 Array-Processing Examples 10 Shuffling a Deck Setting Array Values at Compile Time Ex. Print a random card. String rank = "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ; String suit = "Clubs", "Diamonds", "Hearts", "Spades" ; int i = (int) (Math.random() 13); // between 0 and 12 int j = (int) (Math.random() 4); // between 0 and 3 System.out.println(ranki + " of " + suitj); 12 Setting Array Values at Run Time Ex. Create a deck of playing cards and print them out. typical array-processing code changes values String deck = new String52; at runtime for (int i = 0; i 13; i++) for (int j = 0; j 4; j++) deck4i + j = ranki + " of " + suitj; for (int i = 0; i 52; i++) System.out.println(decki); Q. In what order does it output them? A. B. two of clubs two of clubs two of diamonds three of clubs two of hearts four of clubs two of spades five of clubs three of clubs six of clubs ... ... 13 Shuffling Goal. Given an array, rearrange its elements in random order. Shuffling algorithm.   In iteration i, pick random card from decki through deckN-1, with each card equally likely.   Exchange it with decki. int N = deck.length; for (int i = 0; i N; i++) int r = i + (int) (Math.random() (N-i)); String t = deckr; between i and N-1 swap deckr = decki; idiom decki = t; 14 Shuffling a Deck of Cards: Putting Everything Together public class Deck public static void main(String args) String suit = "Clubs", "Diamonds", "Hearts", "Spades" ; String rank = "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS RANKS; avoid "hardwired" constants build the deck String deck = new StringN; for (int i = 0; i RANKS; i++) for (int j = 0; j SUITS; j++) deckSUITSi + j = ranki + " of " + suitj; for (int i = 0; i N; i++) shuffle int r = i + (int) (Math.random() (N-i)); String t = deckr; deckr = decki; decki = t; print shuffled deck for (int i = 0; i N; i++) System.out.println(decki); 15 Shuffling a Deck of Cards % java Deck % java Deck 5 of Clubs 10 of Diamonds Jack of Hearts King of Spades 9 of Spades 2 of Spades 10 of Spades 3 of Clubs 9 of Clubs 4 of Spades 7 of Spades Queen of Clubs 6 of Diamonds 2 of Hearts 7 of Hearts 7 of Diamonds 7 of Clubs 6 of Spades 4 of Spades Queen of Spades Queen of Diamonds 3 of Spades 10 of Hearts Jack of Diamonds 5 of Diamonds 6 of Diamonds Jack of Clubs 8 of Spades Ace of Hearts 9 of Diamonds ... ... 5 of Spades 10 of Spades 16 Coupon Collector Coupon Collector Problem Coupon collector problem. Given N different card types, how many do you have to collect before you have (at least) one of each type? assuming each possibility is equally likely for each card that you collect Simulation algorithm. Repeatedly choose an integer i between 0 and N-1. Stop when we have at least one card of every type. Q. How to check if we've seen a card of type i? A. Maintain a boolean array so that foundi is true if we've already collected a card of type i. 18 Coupon Collector: Java Implementation public class CouponCollector public static void main(String args) int N = Integer.parseInt(args0); int cardcnt = 0; // number of cards collected int valcnt = 0; // number of distinct cards // do simulation boolean found = new booleanN; while (valcnt N) int val = (int) (Math.random() N); cardcnt++; type of next card if (foundval) (between 0 and N-1) valcnt++; foundval = true; // all N distinct cards found System.out.println(cardcnt); 19 Coupon Collector: Debugging Debugging. Add code to print contents of all variables. Challenge. Debugging with arrays requires tracing many variables. 20 Coupon Collector: Mathematical Context Coupon collector problem. Given N different possible cards, how many do you have to collect before you have (at least) one of each type? Fact. About N (1 + 1/2 + 1/3 + … + 1/N) N ln N. see ORF 245 or COS 340 Ex. N = 30 baseball teams. Expect to wait " 120 years before all teams win a World Series. under idealized assumptions 21