Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.

Similar presentations


Presentation on theme: "Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using."— Presentation transcript:

1 Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01

2 OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using arrays ä Examples ä Accessing arrays sequentially ä Accessing arrays arbitrarily ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using arrays ä Examples ä Accessing arrays sequentially ä Accessing arrays arbitrarily

3 What Are Arrays? ä All of the variables we’ve used so far store a single value each. ä What if we want to store a bunch of related values? ä Example: A deck of cards. ä An array is a structure that stores multiple values, all of which must be of the same type. ä Each individual value is called an element of the array. ä All of the variables we’ve used so far store a single value each. ä What if we want to store a bunch of related values? ä Example: A deck of cards. ä An array is a structure that stores multiple values, all of which must be of the same type. ä Each individual value is called an element of the array. 7 7 2 2 8 8 8 8 9 9 2 2 5 5 7 7 4 4

4 Creating Arrays ä Declaring an array is the same as declaring any normal variable, except you need to add square brackets [].  For example, we would declare an array of ints like this: int[] deckOfCards;  Now deckOfCards can hold any number of ints, not just one. ä Similarly, we can declare arrays of other types:  String[] classRoll; declares an array of Strings. ä Problem: How many elements can the array hold? ä Declaring an array is the same as declaring any normal variable, except you need to add square brackets [].  For example, we would declare an array of ints like this: int[] deckOfCards;  Now deckOfCards can hold any number of ints, not just one. ä Similarly, we can declare arrays of other types:  String[] classRoll; declares an array of Strings. ä Problem: How many elements can the array hold?

5 Creating Arrays ä Arrays are initially of length zero. ä Before we use an array, we must allocate space for it (tell it how many elements we want it to have).  When you allocate space with the new keyword, all elements are given default values (0 for ints ). ä What happens if you try to use an array before allocating space for it? ä Arrays are initially of length zero. ä Before we use an array, we must allocate space for it (tell it how many elements we want it to have).  When you allocate space with the new keyword, all elements are given default values (0 for ints ). ä What happens if you try to use an array before allocating space for it? int[] deckOfCards; deckOfCards = new int[52]; OR int[] deckOfCards = new int[52]; int[] deckOfCards; deckOfCards = new int[52]; OR int[] deckOfCards = new int[52];

6 Using Arrays  Like we saw with Strings, each element of an array has an index, starting from zero: Notice that an array with n elements has indices 0 through n-1. ä So how do we use arrays?  To access the third array element, for example, we write deckOfCards[2].  This now acts as a normal int variable.  Like we saw with Strings, each element of an array has an index, starting from zero: Notice that an array with n elements has indices 0 through n-1. ä So how do we use arrays?  To access the third array element, for example, we write deckOfCards[2].  This now acts as a normal int variable. 7 7 2 2 8 8 8 8 9 9 2 2 5 5 7 7 4 4 0 12 3 45 6 78

7 Using Arrays ä What willthis example do? ä Warning:Make sure you never try to access the n th element of an array that has n elements. ä For example, deckOfCards[52] will give an out-of-bounds error. (Remember, the 52 nd element is deckOfCards[51].) ä This is our first example of a run-time error.  Note: To find the length of deckOfCards, for example, write deckOfCards.length. ä What willthis example do? ä Warning:Make sure you never try to access the n th element of an array that has n elements. ä For example, deckOfCards[52] will give an out-of-bounds error. (Remember, the 52 nd element is deckOfCards[51].) ä This is our first example of a run-time error.  Note: To find the length of deckOfCards, for example, write deckOfCards.length. deckOfCards[3] = 7; deckOfCards[1] = deckOfCards[3]; System.out.println(deckOfCards[1]); deckOfCards[3] = 7; deckOfCards[1] = deckOfCards[3]; System.out.println(deckOfCards[1]);

8 Accessing Arrays Sequentially ä In many cases, you want to look at each array element, one at a time.  A for loop is perfect for this. ä Code example: Summing the elements of an array. ä Code example: Finding the largest element in an array. ä In many cases, you want to look at each array element, one at a time.  A for loop is perfect for this. ä Code example: Summing the elements of an array. ä Code example: Finding the largest element in an array.

9 Accessing Arrays Arbitrarily ä Sometimes you want to access some elements of an array, but not others. Furthermore, you might not want to go in order. ä Example: Counting cards. ä Sometimes you want to access some elements of an array, but not others. Furthermore, you might not want to go in order. ä Example: Counting cards.

10 HomeworkHomework ä Read: 13.1 (to end of p. 545)


Download ppt "Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using."

Similar presentations


Ads by Google