Download presentation
Presentation is loading. Please wait.
Published byAdele Fisher Modified over 8 years ago
1
Arrays Pepper
2
What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String is an array Ex: Array of money earned in the top row of a Jeopardy board: 100-100020030000 0123456
3
Defining an Array int[] intJeopardy = new int[7] 6 is the highest subscript Array holds 7 items [called its length] Note the number used to create it is the length – 1 This would create the exact same array, plus put in initial values: int[] intJeopardy = {0,0,0,0,0,0,0} 100-100020030000 0123456
4
Getting to an Array mini-box x = intJeopardy[1] ; means x will get the value -100 intJeopardy[3] = 500; means the 200 will change to 500 for (int intCount = 0 ; intCount<= 6; intCount++){ intJeopardy[intCount] = 100; } means every value will change to 100 intJeopardy[7] will CRASH 100-100020030000 0123456
5
Getting to an Array mini-box What will the box look like after these statements: intX = 3; intJeopardy[1] = 500; intJeopardy[4] = intX; intJeopardy[intX] = 70; intJeopardy[2+intX] = 600; intJeopardy[6] = intX+5; 100-100020030000 0123456
6
Create your own Array In BlueJ Set up an intJeopardy array with 7 elements and set some values, and then show the values of those elements by printing them: Put 600 in the first box Put 100 in the 4 th box (box 3)
7
Create your own Array Solution In BlueJ Set up an intJeopardy array with 7 elements and set some values, and then show the values of those elements by printing them: int [] intJeopardy = new int[7] intJeopardy[3] = 100 intJeopardy[0] = 600 System.out.println( intJeopardy[3] + “ is the value in box 3”] System.out.println( intJeopardy[0] + “ is the value in box 0”]
8
Initializing an Array How do you set all values in the array to 0? Hint: a for loop from 0 to 6 could give you all the subscripts in the loop 100-100020030000 0123456 for (int intCount = 0 ; intCount<=6; intCount++){ intJeopardy[intCount] = 0; }
9
More using an Array What will this do? System.out.println(“Enter a number”); intJeopardy[4] = scan.NextInt(); 100-100020030000 0123456 Resolve right side: scan.NextInt(); gets a number from the user Put into left side variable Puts the number into the mini-box above #4
10
Total the Value Held Loop through the array mini-boxes adding the values 100-100020030000 0123456 int intTot = 0; for (int intCount = 0; intCount <= 6; intCount++){ intTot = intJeopardy[intCount] + intTot; } System.out.println(“The total money earned in the top row is “ + intTot);
11
Parallel Arrays intJeopardy: Cash Value of each box: intPlayed: Played indicator of each box: Has Jeopardy question 5 been played? Does the 0 in box 5 mean the question hasn’t been played, or that it earned 0 points? Show a message saying the 0 is the score or not 100-100020030000 0123456 True FalseTrue false 0123456 if (intPlayed[5] == true) { System.out.println( intJeopardy[5] + “ is the score” );} else { System.out.println( intJeopardy[5] + “ is not the score”);}
12
Two Dimensional Arrays 100-100020030000 2005006666606300 0123456 0 1 int [][] intJeopardy= new int[1][6] ; intJeopardy[1][4] is value 666 intJeopardy[0][4] is value 200 What is intJeopardy[0][0] value: What is intJeopardy[1][6] value: What is intJeopardy[1][3] value: What is intJeopardy[2][6] value: What is intJeopardy[1][7] value:
13
Array of Objects You can have an array of objects: Player class holds name and score. Player [] objPlayer = new Player[3]; objPlayer[1].name = “Carrie”; objPlayer[1].score = 50; What is objPlayer[3].score? What is objPlayer[0].name? What is objPlayer[2].name? 0123 car rie 50 ma ry 100 am y 90 joh n 0
14
Functions using Arrays An array variable is actually a pointer to the set of boxes, so… – Methods can change the mini-boxes in the main program [though it cannot repoint to another location] Defining in method header: – Use [] after the variable type, and all else the same – Don’t know how big it will be, so leave [] empty public static int[] mymethod(int[] arrayin){
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.