week06

week06 - Week 6: Arrays CS 177 1 What good is a loop...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 177 Week 6: Arrays 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What good is a loop without something to loop over? 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 2
Background image of page 2
Definition of an 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 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Where have you seen arrays before? 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 4
Background image of page 4
Declaration of an array To declare an array of a specified type with a given name : Example with a list of type int : type[] name; int[] list; 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instantiation of an array When you declare an array, you are only creating a variable that can reference an array At first, it references nothing, also known as null To use it, you have to create an array, supplying a specific size: int [] list; list = new int [100]; 6
Background image of page 6
Accessing elements of an array As you have seen with args , you can access an element of an array by an index into it, using square brackets and a number list[9] = 142; System.out.println(list[9]); 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Length of an array 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 8
Background image of page 8
Automatic initialization 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 Type Value int 0 double 0.0 char ‘\0’ boolean false 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Explicit initialization 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”}; int[] numbers = new int[100]; for ( int i = 0; i < numbers.length; i++) numbers[i] = i + 1; 10
Background image of page 10
Memory 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 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A look at memory Suppose that we have an array of type int of length 10
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

week06 - Week 6: Arrays CS 177 1 What good is a loop...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online