1 building java programs chapter 7.1 array basics

13
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

Upload: shanon-sandra-berry

Post on 02-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

1

BUILDING JAVA PROGRAMSCHAPTER 7.1ARRAY BASICS

Page 2: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

2

OBJECTIVES!

• Understand array basics

• Construct an array

• Initialize an array with user-provided values

• Iterate over an array

Page 3: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

3

LET’S GET STARTED!

In your notebook…

1. How is an array constructor different from the other constructors we’ve seen (e.g. for Scanner)?

2. We’ve actually seen an array before, but it was a part of a “magic incantation”. Where was it?

Page 4: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

4

WHAT 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];

Page 5: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

5

ALTERNATE CONSTRUCTION

In 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]

Page 6: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

6

USING AN ARRAY…

Since 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.

What’s the difference?

You don’t 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

Page 7: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

7

USING AN ARRAY…

To reference a specific element in the array, provide the index (remember it’s 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

Page 8: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

8

ARRAYS AS PARAMETERS…

When 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!

Page 9: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

9

ARRAYS AS PARAMETERS…

What 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!

Page 10: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

10

FOR LOOPS AND FOR-EACH LOOPS

We 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 myArray…”

int[] myArray = {8, 42, 13, 77, 8, 16};

for (int i : myArray) { System.out.println(i);}

Page 11: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

11

FOR-EACH LOOPS…

• The only catch with for-each loops is that you don’t know where in the array you are… so you can’t do anything special for the first or last element!

• You also can’t 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.

Page 12: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

12

LET’S TRY IT!

Write a program (you don’t 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

Page 13: 1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

13

WHAT WE COVERED

• You should now understand how an array generally works

• You should be able to construct an array

• You should be able to initialize an array with a set of values

• You should be able to iterate over an array