Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size...

41
CSCI1226 Introduction to Computing Science and Programming Lecture 7: Arrays Tami Meredith

description

What is data? Data are values that are stored in memory Memory locations are given names Those names are called identifiers A named location is called a variable A type describes the contents of the memory location in general terms so we can make the location the correct size Thus, we have: 1. Memory locations – of some size 2. Types – to tell us things about the value we will store 3. Names – to identify the locations 4. Values – to put in the locations

Transcript of Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size...

Lecture 7: Arrays Tami Meredith Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array What is data? Data are values that are stored in memory Memory locations are given names Those names are called identifiers A named location is called a variable A type describes the contents of the memory location in general terms so we can make the location the correct size Thus, we have: 1. Memory locations of some size 2. Types to tell us things about the value we will store 3. Names to identify the locations 4. Values to put in the locations Variables 1. int i; Defines a section of memory large enough to hold an integer and names that area of memory " i ", the memory contains a random value from whatever it was used for previously 2. i = 5; Stores the value 5 in the area or memory named " i " 3. int i = 5; Combines 1. and 2. A variable holds one, and only one, thing A variable always has a value... if you put nothing there then assume it has a random value Groups of Variables A block lets us group statements so that they can be used where a single statement is needed An array lets us group variables so that we can store groups of similar values with a single name An array is a set of adjacent (side by side) memory locations We use square brackets, " [ " and " ] " when working with arrays Aside: Delimiters [ ] Something to do with an array " " A string (sequence of zero or more characters) 'a' A character (a single character, never empty) { } A block (sequence of zero or more statements) /* */ A multi-line comment ( ) A math expression A test in a loop or conditional The parameters of a method call The parameters in a method declaration Arrays 4 integers stored (somewhere) in memory int a, b, c, d; An array named " e " that stores 4 integers int[] e = new int[4]; a dcb e[0]e[1]e[2]e[3] Array Syntax: Definition int[] e = new int[4]; type [] name = new type [ number-of-elements ]; type tells us how big each slot needs to be and what it will hold name is what the entire array is called new is a special keyword that means "assign memory" and is always followed by an indicator of how much memory to get (in this case, enough for 4 integers) Array Syntax: Assignment int[] coords = new int[2]; coords[0] = 3; coords[1] = 5; 0 and 1 are referred to as array indices Arrays are indexed from 0 because the index identifies how many slots we are from the front of the array An index is not a count into the array! (Its actually an offset) Same as Strings (c.f., arrays of characters) coords[0]coords[1] 35 new 1. int x; 2. x = 4; 1. int[] y; 2. y = new int[4]; 3. y[0] = 1; y[1] = 2; y[2] = 4; y[3] = 8; Arrays add the extra step of indicating how many values the array needs to store (and getting the memory for it at that time using new ). x 4 y[0]y[1]y[2]y[3] 1248 new (Part 2) 1. int[] y; 2. y = new int[4]; Step 1 creates a memory slot called y Step 2 creates 4 memory slots called y[0], y[1], y[2], a nd y[3] y stores a. Information to access y[0] to y[3] b. A count of then number of slots created y is secretly an object, it stores a group of data Indices Strings are indexed from 0 Arrays are indexed from 0 We often have called the counter in a loop as the index value We do this because we frequently use loops to process all the elements (i.e., the value at each index) of arrays and Strings Really, a String is just an array of characters with some special rules Indices We can use a variable to indicate an index value int x = 0, y = 1; int[] coords = new int[2]; coords[x] = 3; // same as coords[0] coords[y] = 5; // same as coords[1] Arrays and Loops Arrays work amazingly well with loops! Let us see how to get four integers from a user... int[] vals = new int[4]; int i; Scanner user = new Scanner(System.in); for (i = 0; i = 0; i--) { data[i] = i; } Arrays.sort(data); } Now What? No more additional chapters!! Now we go back and start: 1. Reviewing 2. Covering things we skipped 3. Consolidating concepts Now we can slow down and enjoy the scenery! To Do Go to the lab and complete Assignment 6 Read Chapter 7 if you have not done so already! Go back and start re-reading Chapters 1-6 until you *REALLY* understand the material in them Practice, Practice, Practice! Program until your eyeballs fall out! (It is covered under your student health plan, I checked).