week06

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

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

CS 177 Week 6: Arrays 1

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
A look at memory Suppose that we have an array of type int of length 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 44

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

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

View Full Document
Ask a homework question - tutors are online