Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 6 - Friday.  What did we talk about last time?  Loop examples."— Presentation transcript:

1 Week 6 - Friday

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

3

4

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

6  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

7

8  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

9  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

10  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

11

12  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;

13  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];

14  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]);

15  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

16  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

17  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);

18

19  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

20  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 524 1243-967890-2323106 0 1 2 3 4 5 6 7 8 9 524 528 532 536 540 544 548 552 556 560 Addresses Indexes

21

22  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

23  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];

24

25

26  More on arrays  StdDraw

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


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

Similar presentations


Ads by Google