Post on 02-Jan-2016




0 download

Embed Size (px)


Chapter 7.1 Array Basics

Building Java ProgramsChapter 7.1Array Basics#Objectives!Understand array basicsConstruct an arrayInitialize an array with user-provided valuesIterate over an array#Lets get started!In your notebookHow is an array constructor different from the other constructors weve seen (e.g. for Scanner)?Weve actually seen an array before, but it was a part of a magic incantation. Where was it?#1 minute into class to think2 minutes to talk with neighbor2 minutes to share as a class3What is an array?An array is a fixed-size list of items of a particular type.It is zero-indexed, just like a String.It is an object, not a primitive type, so it has to be constructed before it can be used:

When a new array is constructed, all of the items are automatically initialized to zero (or a zero-equivalent value, like false or null).int[] myArray = new int[20];#Alternate constructionIn addition to using new, you can provide an array initializer much like initializing a String to a string literal:

Remember to start and end the list with curly braces!String myString = "hello!";int[] myArray = {8, 42, 13, 77, 8, 16};

// myArray has 6 elements, just like doing:// new int[6]#Using an arraySince an array has a fixed size, Java provides a way to find out its length. This is almost the same as getting the length of a String, with one important difference.Whats the difference?You dont put parentheses after the .length when getting the length of an array:String myString = "hello!";int[] myArray = {8, 42, 13, 77, 8, 16};int strLen = myString.length(); // evaluates to 6int arrLen = myArray.length; // evaluates to 6#Using an arrayTo reference a specific element in the array, provide the index (remember its zero-based!) in square brackets after the array variable.This is similar to charAt() on a String:String myString = "hello!";int[] myArray = {8, 42, 13, 77, 8, 16};char c = myString.charAt(1); // c gets 'e'int n = myArray[1]; // n gets 42#Arrays as parametersWhen you pass an array as a parameter to a method, you actually pass a reference to it. This is because the array is an object, not a primitive type.This means that a method can change the elements of an array.This is very different than the behavior with primitive types and takes some getting used to!#Arrays as parametersWhat does this display?

public static void main() { int x = 1; int[] y = {1};

changeValues(x, y);

System.out.println("x => " + x + ", y[0] => " + y[0]);}

public static void changeValues(int a, int[] b) { a++; b[0]++;}x => 1, y[0] => 2// does change the element in the caller!#For loops and For-each loopsWe already know about for loops, and those work just as they always have.Java also has a special loop for arrays if all you want to do is do something with each element value: the for-each loop:

Read this as for each i in myArrayint[] myArray = {8, 42, 13, 77, 8, 16};

for (int i : myArray) { System.out.println(i);}#For-each loopsThe only catch with for-each loops is that you dont know where in the array you are so you cant do anything special for the first or last element!

You also cant do anything based on the current index (like comparing or changing the next or previous values).

This catch makes the for-each loop only useful in certain situations. A regular for loop is much more flexible, and is always okay to use instead.#Lets try it!Write a program (you dont have to use any methods other than main!) that asks for a certain number of words, then prints the average length and the number of words whose length exceeds the average:

Before you start what approach would you use?How many words? 5Word #1? iceWord #2? creamWord #3? socialWord #4? sprinklesWord #5? chocolateAverage word length: 6.4Number of words longer than average: 2#What we coveredYou should now understand how an array generally worksYou should be able to construct an arrayYou should be able to initialize an array with a set of valuesYou should be able to iterate over an array#