7 Array

7 Array - Arrays Arrays An array is a collection of data,...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Arrays Arrays An array is a collection of data, all of the same data type, that is accessed with an index If a variable is like a mailbox, an array is like the mailboxes at a post office or an apartment building: a row of otherwise identical boxes with different numbers. (square brackets) are used to indicate an (square brackets) are used to indicate an array 1 Arrays Arrays int nums = {5,-4,17}; nums 5 ­4 17 [0] [1] [2] 2 Arrays Arrays May be declared and initialized in one line. can go either before or after the name of the can go either before or after the name of the array int nums = {5,-4,17}; int //or //or int nums = {5,-4,17}; int Are "zero­based": First index is 0 println(nums[1]); //displays -4 println(nums[1]); You can make an array of any data type char letters = {'a', 'b', 'c'}; char 3 String answers = {“yes”, “no”}; String Arrays Arrays Just like any variable, you can change the value that is stored there char letters = {'a', 'b', 'c'}; println(letters[0]); letters[0]='z'; println(letters[0]); 4 A Simple Program using an A Simple Program using an array float nums = {5, 1.5, 7, 3.2}; int i = 3; while(i >= 0) { println(nums[i]); println(nums[i]); i--; i--; } 5 A Simple Program using an A Simple Program using an array float nums = {5, 1.5, 7, 3.2}; int i = 3; while(i >= 0) { println(nums[i]); println(nums[i]); i--; i--; } 6 Don't confuse the index with the Don't confuse the value that is stored there float nums = {5, 1.5, 7, 3.2}; nums[2] 2 is the index (think apartment number) is the 7 is the value that is stored at index 2 7 Practice Quiz Question: What is Practice Quiz Question: What is the output of this program? String words = { "The", "Quick", "Brown","Fox", "Jumped", "Over", "The", "Lazy", "Red", "Dog"}; "Lazy", words[2] = "Blue"; for(int i = 0;i < 10;i=i+2) { println(words[i]); } 8 Practice Quiz Question: What is Practice Quiz Question: What is the output of this program? String words = { "The", "Quick", "Brown","Fox", "Jumped", "Over", "The", "Lazy", "Red", "Dog"}; "Lazy", words[2] = "Blue"; for(int i = 0;i < 10;i=i+2) { println(words[i]); } 9 Practice Quiz Question: What is Practice Quiz Question: What is the output of this program? String words = { "The", "Quick", "Brown","Fox", "Jumped", "Over", "The", "Lazy", "Red", "Dog"}; "Lazy", words[2] = "Blue"; int i = 0; while(i < 10) { println(words[i]); println(words[i]); i=i+2; i=i+2; } 10 The length of an array The The length of an array, is the number of elements that array can hold Think of it as the number of "apartments" in that array char letters = {'d','o','g','s'}; println(letters.length); 11 The length of an array The The length of an array, is the number of elements that array can hold Think of it as the number of "apartments" in that array char letters = {'d','o','g','s'}; println(letters.length); 12 The length of an array The length is often used with loops that go through each element in an array char letters = {'d','o','g','s'}; int i = 0; while(i < letters.length) { print(letters[i]); print(letters[i]); i++; i++; } 13 The length of an array The length is often used with loops that go through each element in an array char letters = {'d','o','g','s'}; int i = 0; while(i < letters.length) { print(letters[i]); print(letters[i]); i++; i++; } 14 the int() function the Java is very picky about types: int num = 3.14; generates an error 15 the int() function the We can fix the error with the int() function int num = int(3.14); int int It drops the decimals (called "truncation"); 16 Practice Quiz Question: Practice Quiz Question: What is the output? float nums = {3.14,4.999,2.718,0.11235813}; println("There are "+ nums.length + " elements"); for(int i = 0; i < nums.length; i++) { int nNum = int(nums[i]); int println(nNum); println(nNum); } 17 ...
View Full Document

Ask a homework question - tutors are online