Week 6 - Friday.  What did we talk about last time?  Loop examples.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
A First Book of ANSI C Fourth Edition
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CS107 References and Arrays By Chris Pable Spring 2009.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Heads-Up – Programming Quiz This Week Announcement will be made via You will be provided with a login to the password-protected website You will.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Arrays.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
int [] scores = new int [10];
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Week 7 - Wednesday CS 121.
Week 15 – Wednesday CS 121.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
Object Oriented Programming in java
int [] scores = new int [10];
Week 7 - Monday CS 121.
Presentation transcript:

Week 6 - Friday

 What did we talk about last time?  Loop examples

do { statement1; statement2; … statementn; } while( condition );

 I said that the do-while loop is rarely used, but certain kinds of input are well suited to a do-while  For example, what about a program that gives a menu of options 1. Add two numbers 2. Subtract two numbers 3. Multiply two numbers 4. Quit

 Loops are great  But, without a way to talk about a list of variables, we can’t get the full potential out of a loop  Enter: the array

 An array is a homogeneous, static data structure  Homogeneous means that everything in the array is the same type: int, double, String, etc.  Static (in this case) means that the size of the array is fixed when you create it

 The args variable passed into the main() method is an array of type String  This array has a set number of String s (maybe zero) that you can use as input to your program  Now, we are giving you the ability to create and manipulate your own arrays

 To declare an array of a specified type with a given name :  Example with a list of type int :  Just like any variable declaration, but with [] type[] name; int[] list;

 When you declare an array, you are only creating a variable that can hold an array  At first, it holds nothing, also know as null  To use it, you have to create an array, supplying a specific size:  This code creates an array of 100 int s int[] list; list = new int[100]; int[] list; list = new int[100];

 You can access an element of an array by indexing into it, using square brackets and a number  Once you have indexed into an array, that variable behaves exactly like any other variable of that type  You can read values from it and store values into it  Indexing starts at 0 and stops at 1 less than the length list[9] = 142; System.out.println(list[9]); list[9] = 142; System.out.println(list[9]);

 When you instantiate an array, you specify the length  Sometimes (like in the case of args ) you are given an array of unknown length  You can use its length member to find out int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42 int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42

 When you create an int, double, char, or boolean array, the array is automatically filled with certain values  For other types, including String s, each index in the array must be filled explicitly TypeValue int0 double0.0 char'\0' booleanfalse

 Explicit initialization can be done with a list:  Or, a loop could be used to set all the values: String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; String[] numbers = new String[100]; for(int i = 0; i < numbers.length; i++) numbers[i] = "" + (i + 1); String[] numbers = new String[100]; for(int i = 0; i < numbers.length; i++) numbers[i] = "" + (i + 1);

 An array takes up the size of each element times the length of the array  Each array starts at some point in computer memory  The index used for the array is actually an offset from that starting point  That’s why the first element is at index 0

 We can imagine that we have an array of type int of length 10  Java decides what address in memory is going to be used, but let’s say it starts at Addresses Indexes

 Arrays are a fixed size list of a single kind of data  A for loop is ideal for iterating over every item and performing some operation  for loops and arrays will crop up again and again  Of course, a while loop can be used with an array, but it rarely is

 Imagine that we have an array of int s called list  Let’s use a for loop to sum up those int s  Super easy, just like before  We don’t even need to know how big the array is ahead of time int sum = 0; for( int i = 0; i < list.length; i++ ) sum += list[i]; int sum = 0; for( int i = 0; i < list.length; i++ ) sum += list[i];

 More on arrays  StdDraw

 Keep reading Chapter 6 of the textbook  Keep working on Project 2