Programming Fundamentals I CS 110 Central Washington University

  • Slides: 37
Download presentation
Programming Fundamentals I CS 110, Central Washington University Spring 2017 CS 110: Programming Fundamentals

Programming Fundamentals I CS 110, Central Washington University Spring 2017 CS 110: Programming Fundamentals I, Spring 2017 1 12/15/2021

Announcements … towards the end of the quarter. . . Upcoming Dates May 25:

Announcements … towards the end of the quarter. . . Upcoming Dates May 25: May 30: Homework #6 due Final project due If you save it to the last minute, you risk not getting it done (and/or some upload glitch, occurring), and it interfering with your final exams…) Final exam: 6/7/17, Wednesday, 2 pm – 4 pm, H 112 June 11: Final grades will be calculated CS 110: Programming Fundamentals I, Spring 2017 2 12/15/2021

Announcements … Final Project README. TXT file (5 points of the final project score)

Announcements … Final Project README. TXT file (5 points of the final project score) An industry practice is to include such a file in a directory with your code Traditionally the file's name is written in upper case so that on case-preserving environments the file will appear near the beginning of a directory listing Contents of the file may include: configuration instructions installation details operating instructions contact information known bugs Etc. CS 110: Programming Fundamentals I, Spring 2017 3 12/15/2021

Announcements … Final Project README. TXT file (5 points of the final project score)

Announcements … Final Project README. TXT file (5 points of the final project score) Another example: . CS 110: Programming Fundamentals I, Spring 2017 4 12/15/2021

Today Array bounds Initializing Arrays Copying Arrays Processing/Accessing Arrays The Enhanced for loop CS

Today Array bounds Initializing Arrays Copying Arrays Processing/Accessing Arrays The Enhanced for loop CS 110: Programming Fundamentals I, Spring 2017 5 12/15/2021

Array Bounds. . . The length field of an array object indicates how many

Array Bounds. . . The length field of an array object indicates how many elements are in the array. Because array indexes begin at 0, be careful about an array's “length” double[] my 3 Nums; my 3 Nums = new double[3]; my 3 Nums[0] = 2. 0; my 3 Nums[2] = 3. 0; System. out. println(my 3 Nums. length); This is a constant, that contains the length of the array object … hence we don't use (), which is for methods Instance of Class Arrays Field: int length = 3 my 3 Nums Methods: CS 110: Programming Fundamentals I, Spring 2017 6 file(); sort(); Binary. Search(); … …. . . 12/15/2021

Array Bounds … accessing an array's value(s) Using an array's “length” constant, we can

Array Bounds … accessing an array's value(s) Using an array's “length” constant, we can “walk over” an array's entries double[] my 4 Numbers; my 4 Numbers = new double[4]; my 4 Numbers[2] = 2. 0; my 4 Numbers[3] = 3. 0; for (int i = 0; i<my 4 Numbers. length; i++){ System. out. println(my 4 Numbers[i]); } declare reference variable, my 4 Numbers, to refer to an array of doubles Set value at index position 2 to 2. 0 Create for loop, iterate with i, making sure that i for each iteration is less than 4 Set value at index position 3 to 3. 0 my 4 Numbers 0. 0 2. 0 0. 0 1 2 3. 0 0. 0 i 0 CS 110: Programming Fundamentals I, Spring 2017 7 3 For-loop terminates 12/15/2021

Array Initialization. . . Arrays can be initialized “entry-by-entry” int[] my 3 Numbers; my

Array Initialization. . . Arrays can be initialized “entry-by-entry” int[] my 3 Numbers; my 3 Numbers = new int[3]; my 3 Numbers[1] = 1; my 3 Numbers[2] = -7; my 3 Numbers[0] = -7; Notice that: q q entries do NOT have to be updated “in order” In the above code, first the value at index position 1 is updated In the above code, the value at index position 2 is updated next In the above code, the value at index position 0 is updated last CS 110: Programming Fundamentals I, Spring 2017 8 12/15/2021

Array Initialization. . . … or they can be initialized using an initialization list.

Array Initialization. . . … or they can be initialized using an initialization list. . . int[] my 3 Numbers = {1, -7}; When using an initialization list, the values in the list are stored in the array in order The above statement achieves several things: q q q Reference variable my 3 Numbers is declared A new array of three integers is created The array's entries are set to 1, -7, in that order CS 110: Programming Fundamentals I, Spring 2017 9 12/15/2021

Array Initialization. . . … or they can be initialized using an initialization list.

Array Initialization. . . … or they can be initialized using an initialization list. . . int[] my 3 Numbers = {1, -7}; Commas separate elements The entries in the array must be of the correct type CS 110: Programming Fundamentals I, Spring 2017 10 12/15/2021

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY data type … we've seen arrays of ints and doubles … but we can also have char[] my. Characters; my. Characters = new char[5]; my. Characters[4] = 'b'; my. Characters[3] = 'c'; for (int i = 0; i <= my. Characters. length; i++){ System. out. println(my. Characters[i]); } Q: After the first two lines of code, what “exists” in memory? Q: This line of code achieves. . . my. Characters A reference variable … that refers to. . . CS 110: Programming Fundamentals I, Spring 2017 … an array with 5 items, each of type char, which have been initialized to the empty character 11 12/15/2021

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY data type … we've seen arrays of ints and doubles … but we can also have char[] my. Characters; my. Characters = new char[5]; my. Characters[4] = 'b'; my. Characters[3] = 'c'; for (int i = 0; i <= my. Characters. length; i++){ System. out. println(my. Characters[i]); } Q: The third line of code accomplishes. . . Q: Next line of code accomplishes. . . … and the fourth line of code sets the value at index position 3 to a c b c my. Characters 0 1 2 3 4 The my. Characters array's first index is 0, which is the first item in the array… … so the item at the index position 4 is the fifth item in the array my. Characters … therefore third line of code sets the value at index position 4 to a b CS 110: Programming Fundamentals I, Spring 2017 12 12/15/2021

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY

Arrays of chars As mentioned in lecture, arrays can hold data items of ANY data type … we've seen arrays of ints and doubles … but we can also have char[] my. Characters; my. Characters = new char[5]; my. Characters[4] = 'b'; my. Characters[3] = 'c'; for (int i = 0; i <= my. Characters. length; i++){ System. out. println(my. Characters[i]); } To fix this logical error, remember that when iterating over an Q: What will this for-loop accomplish? array, the index of the array's last item has a value that is one less than the length of the array. Q: So, change <= to _______ or or<? increment i to a value 1 less than the length of my. Characters c b my. Characters 0 1 2 3 4 …. NOTHING, because of a logical error …. the length property of the my. Characters array object is 5, and when i=5, the for loop's body will be executed…which will cause the program to crash, because there is NO index position 5. CS 110: Programming Fundamentals I, Spring 2017 13 12/15/2021

Poll Question … arrays 1 public class Array. Example 3 { 2 3 //

Poll Question … arrays 1 public class Array. Example 3 { 2 3 // The main method 4 public static void main(String[] args){ 5 6 double[] some. Silly. Nums; 7 some. Silly. Nums = new double[3]; 8 some. Silly. Nums[0] = 33. 4; 9 some. Silly. Nums[2] = 44. 3; 10 11 for (int i = 0; i <= some. Silly. Nums. length - 2; i++){ 12 System. out. println(some. Silly. Nums[i+1]); 13 }}} For the above java code, which of the following statements is/are true? I. III. IV. There is a syntax error on line 7, because double[3] should be double[3. 0] There is a syntax error on line 12, because array position i+1 will crash the program There is a syntax error on line 11, because <= should be < This program prints 0. 0, followed by 44. 3 A. Choice I only. C D. Choice IV only G. Choices III and IV only B. Choice II only E. Choices I and II only H. Choices I, II, and IIII only C. Choice III only F. Choices II and III only Step-by-step execution of the above code … this is non-trivial example … go over these slides by yourself, slowly. . . good practice for the final exam CS 110: Programming Fundamentals I, Spring 2017 14 12/15/2021

Poll Question … arrays 1 public class Array. Example 3 { 2 Set the

Poll Question … arrays 1 public class Array. Example 3 { 2 Set the value at index position 0 of 3 // The main method some. Silly. Nums to the value 33. 4 4 public static void main(String[] args){ 5 Set the value at index position 2 of 6 double[] some. Silly. Nums; some. Silly. Nums to the value 44. 3 7 some. Silly. Nums = new double[3]; 8 some. Silly. Nums[0] = 33. 4; 9 some. Silly. Nums[2] = 44. 3; 10 11 for (int i = 0; i <= some. Silly. Nums. length - 2; i++){ 12 System. out. println(some. Silly. Nums[i+1]); 13 }}} Create a reference variable, some. Silly. Nums, that refers to an array of doubles Create a new array object with space for three doubles, initialized to zeros, and set some. Silly. Nums to refer to it some. Silly. Nums 0. 0 3. 4 0. 0 44. 3 0. 0 For the for loop, “step” through its execution to see what is printed CS 110: Programming Fundamentals I, Spring 2017 15 12/15/2021

Poll Question … arrays 1 public class Array. Example 3 { 2 Set the

Poll Question … arrays 1 public class Array. Example 3 { 2 Set the value at index position 0 of 3 // The main method 4 public static void main(String[] args){ some. Silly. Nums to the value 33. 4 5 Set the value at index position 2 of 6 double[] some. Silly. Nums; some. Silly. Nums to the value 44. 3 7 some. Silly. Nums = new double[3]; 8 some. Silly. Nums[0] = 33. 4; 9 some. Silly. Nums[2] = 44. 3; 10 11 for (int i = 0; i <= some. Silly. Nums. length - 2; i++){ 12 System. out. println(some. Silly. Nums[i+1]); 13 }}} Create a reference variable, some. Silly. Nums, that refers to an array of doubles Create a new array object with space for three doubles, initialized to zeros, and set some. Silly. Nums to refer to it some. Silly. Nums 0. 0 3. 4 0. 0 44. 3 0. 0 For the for loop, “step” through its execution to see what is printed CS 110: Programming Fundamentals I, Spring 2017 16 12/15/2021

Poll Question … arrays 1 public class Array. Example 3 { 2 3 //

Poll Question … arrays 1 public class Array. Example 3 { 2 3 // The main method 4 public static void main(String[] args){ 5 6 double[] some. Silly. Nums; 7 some. Silly. Nums = new double[3]; 8 some. Silly. Nums[0] = 33. 4; 9 some. Silly. Nums[2] = 44. 3; 10 11 for (int i = 0; i <= some. Silly. Nums. length - 2; i++){ 12 System. out. println(some. Silly. Nums[i+1]); 13 }}} 33. 4 44. 3 0. 0 some. Silly. Nums For the for loop, “step” through its execution to see what is printed value of i for condition is true? position output 0 yes 1 0. 0 1 yes 2 44. 3 2 no, loop terminates CS 110: Programming Fundamentals I, Spring 2017 17 12/15/2021

1 public class Array. Example 4 { Arrays 2 public static void main(String[] args){

1 public class Array. Example 4 { Arrays 2 public static void main(String[] args){ 3 4 int my. First. Digit = -753; 5 int my. Second. Digit = -44; 6 int my. Third. Digit = 1459; 7 int my. Fourth. Digit = 1754; 8 int my. Fifth. Digit = 1916; 9 int my. Sixth. Digit = 1944; 10 11 int[] six. Digits; 12 six. Digits = new int[6]; 13 six. Digits[0] = -753; 14 six. Digits[1] = -44; 15 six. Digits[2] = 1459; 16 six. Digits[3] = 1754; 17 six. Digits[4] = 1916; 18 six. Digits[5] = 1944; 19 20 int[] my. Six. Digits = {-753, -44, 1459, 1754, 1916, 1944}; 21 22 // to sum variables on lines 4 -9 23 System. out. println(my. First. Digit + my. Second. Digit + my. Third. Digit 24 + my. Fourth. Digit + my. Fifth. Digit + my. Sixth. Digit); 25 26 // to sum integers in array six. Digits 27 int sum. Of. Vals. In. Array = 0; 28 for (int i = 0; i < six. Digits. length; i++){ 29 sum. Of. Vals. In. Array += six. Digits[i]; 30 } 31 System. out. println(sum. Of. Vals. In. Array); 32 }} CS 110: Programming Fundamentals I, Spring 2017 18 … are you convinced … yet? Approach 1 Create 6 variables Approach 2 Create array with 6 elements Approach 3 Create array using list declaration Each of these three approaches create identical pieces of data for later retrieval/manipulation … but Approaches 1 and 2 require much more coding than does Approach 3. Now assume you want to sum the 6 digits -753, -44, 1459, 1754, 1916, 1944 12/15/2021

1 public class Array. Example 4 { Arrays 2 public static void main(String[] args){

1 public class Array. Example 4 { Arrays 2 public static void main(String[] args){ 3 4 int my. First. Digit = -753; 5 int my. Second. Digit = -44; 6 int my. Third. Digit = 1459; 7 int my. Fourth. Digit = 1754; 8 int my. Fifth. Digit = 1916; 9 int my. Sixth. Digit = 1944; 10 11 int[] six. Digits; 12 six. Digits = new int[6]; 13 six. Digits[0] = -753; 14 six. Digits[1] = -44; 15 six. Digits[2] = 1459; 16 six. Digits[3] = 1754; 17 six. Digits[4] = 1916; 18 six. Digits[5] = 1944; 19 20 int[] my. Six. Digits = {-753, -44, 1459, 1754, 1916, 1944}; 21 22 // to sum variables on lines 4 -9 23 System. out. println(my. First. Digit + my. Second. Digit + my. Third. Digit 24 + my. Fourth. Digit + my. Fifth. Digit + my. Sixth. Digit); 25 26 // to sum integers in array six. Digits 27 int sum. Of. Vals. In. Array = 0; 28 for (int i = 0; i < six. Digits. length; i++){ 29 sum. Of. Vals. In. Array += six. Digits[i]; 30 } 31 System. out. println(sum. Of. Vals. In. Array); 32 }} CS 110: Programming Fundamentals I, Spring 2017 19 … are you convinced … yet? Approach 1 Create 6 variables Approach 2 Create array with 6 elements Approach 3 Create array using list declaration Print out the sum of the 6 variables defined using method 1 This summing works, but there are many variable names that may be misspelled. And, what if you had to sum 400 numbers? Print out the sum of the 6 items in the array six. Digits The second way of summing is easier to manage and maintain, etc. 12/15/2021

Processing/Accessing Array Contents Another example. . . 1 public class Array. Example 5 {

Processing/Accessing Array Contents Another example. . . 1 public class Array. Example 5 { 2 public static void main(String[] args){ 3 4 int[] a. Handful. Of. Digits = {2, 9, 16, 25, 36, 49}; 5 6 // Array values can be used as any other variable 7 int sum. Of. First. Two = a. Handful. Of. Digits[0] 8 + a. Handful. Of. Digits[1]; 9 System. out. println("Sum of first two: " 10 + sum. Of. First. Two); 11 12 // Array elements can be used in relational ops 13 if (a. Handful. Of. Digits[3] < a. Handful. Of. Digits[0]){ 14 System. out. println("First elements larger " + 15 "than fourth"); 16 } 17 18 // Array elements can also be used as loop 19 int my. Counter = -7; 20 while (a. Handful. Of. Digits[3] == 25){ 21 if (my. Counter == 0) 22 a. Handful. Of. Digits[3] = 65; 23 System. out. println("Value of a. Handful. Of. Digits[3] : " 24 + a. Handful. Of. Digits[3]); 25 my. Counter++; 26 }}} CS 110: Programming Fundamentals I, Spring 2017 20 What does this statement do? Here, the values at index positions 0 and 1 are summed, and the result is placed into sum. Of. First. Two Output? The values of array elements can also be used in conditional statements or any other type of logical statements. . . The System. out. println statement will be executed ONLY if the first element (index position 0) of the array is larger than the fourth element (index position 3) Array elements can also be used as loop conditionals 12/15/2021

Processing/Accessing Array Contents 1 public class Array. Example 5 { 2 public static void

Processing/Accessing Array Contents 1 public class Array. Example 5 { 2 public static void main(String[] args){ 3 4 int[] a. Handful. Of. Digits = {2, 9, 16, 25, 36, 49}; 5 6 // Array values can be used as any other variable 7 int sum. Of. First. Two = a. Handful. Of. Digits[0] 8 + a. Handful. Of. Digits[1]; 9 System. out. println("Sum of first two: " 10 + sum. Of. First. Two); 11 12 // Array elements can be used in relational ops 13 if (a. Handful. Of. Digits[3] < a. Handful. Of. Digits[0]){ 14 System. out. println("First elements larger " + 15 "than fourth"); 16 } 17 18 // Array elements can also be used as loop 19 int my. Counter = -7; 20 while (a. Handful. Of. Digits[3] == 25){ 21 if (my. Counter == 0) 22 a. Handful. Of. Digits[3] = 65; 23 System. out. println("Value of a. Handful. Of. Digits[3] : " 24 + a. Handful. Of. Digits[3]); 25 my. Counter++; 26 }}} CS 110: Programming Fundamentals I, Spring 2017 21 The program on the left will print how many lines of text to the console? A. B. C. D. E. F. G. H. 2 3 4 5 6 7 8 9 First Line Printed Sum of first two: 11 This will NOT be invoked because the first element of the array is NOT greater than the fourth Second Line Printed Value of a. Handful. Of. Digits[3] : 25 my. Counter = -7 12/15/2021

The program on the left will print how many lines of text to the

The program on the left will print how many lines of text to the console? Processing/Accessing Array Contents 1 public class Array. Example 5 { 2 public static void main(String[] args){ A. 3 4 int[] a. Handful. Of. Digits = {2, 9, 16, 25, 36, 49}; B. 5 C. 6 // Array values can be used as any other variable D. 7 int sum. Of. First. Two = a. Handful. Of. Digits[0] E. 8 + a. Handful. Of. Digits[1]; F. 9 System. out. println("Sum of first two: " G. 10 + sum. Of. First. Two); H. 11 a. Handful. Of. Digits[3] my. Counter 12 // Array elements can be used in relational ops 13 if (a. Handful. Of. Digits[3] < a. Handful. Of. Digits[0]){ 25 -7 14 System. out. println("First elements larger " + 15 "than fourth"); 25 -6 16 } 25 -5 17 18 // Array elements can also be used as loop 25 -4 19 int my. Counter = -7; 25 -3 20 while (a. Handful. Of. Digits[3] == 25){ 21 if (my. Counter == 0) 25 -2 22 a. Handful. Of. Digits[3] = 65; 25 -1 23 System. out. println("Value of a. Handful. Of. Digits[3] : " 24 + a. Handful. Of. Digits[3]); 25 0 25 my. Counter++; 65 1 26 }}} CS 110: Programming Fundamentals I, Spring 2017 22 2 3 4 5 6 7 8 9 Line printed 2 3 4 5 6 7 8 9 Done! 12/15/2021

The Enhanced For Loop CS 110: Programming Fundamentals I, Spring 2017 23 12/15/2021

The Enhanced For Loop CS 110: Programming Fundamentals I, Spring 2017 23 12/15/2021

The Enhanced For Loop We've seen how a for loop, along with the length

The Enhanced For Loop We've seen how a for loop, along with the length field of an array object, can be used to Walk Over an array, one element at a time 1 public class Array. Example 6 { 2 public static void main(String[] args){ 3 4 double[] my. Numbers = {1. 11, 2. 22, 3. 33}; 5 6 for (int i=0; I < my. Numbers. length; i++){ 7 System. out. println("The array entry : " + my. Numbers[i]); 8 } 9 10 for(double val : my. Numbers){ 11 System. out. println("The value is " + val); 12 } 13 }} Q: What does the above program output to the screen? This for loop will access each index position, starting from 0 to 1 less than length, and the output will be: The array entry : 1. 11 The array entry : 2. 22 The array entry : 3. 33 There is another, simplified way of processing (read-only) an array 24 CS 110: Programming Fundamentals I, Spring 2017 12/15/2021

The Enhanced For Loop We've seen how a for loop, along with the length

The Enhanced For Loop We've seen how a for loop, along with the length field of an array object, can be used to Walk Over an array, one element at a time 1 public class Array. Example 6 { 2 public static void main(String[] args){ 3 4 double[] my. Numbers = {1. 11, 2. 22, 3. 33}; 5 6 for (int i=0; I < my. Numbers. length; i++){ 7 System. out. println("The array entry : " + my. Numbers[i]); 8 } 9 10 for(double val : my. Numbers){ 11 System. out. println("The value is " + val); 12 } 13 }} The enhanced for loop always goes through ALL of the elements of an array, is read-only (cannot be used to update the values in an array), and has the general format: for (datatype element. Variable : array. Name){ statement; } CS 110: Programming Fundamentals I, Spring 2017 25 This enhanced for loop will print … The value is 1. 11 The value is 2. 22 The value is 3. 33

Reassigning Array References An array reference can be assigned to another array of the

Reassigning Array References An array reference can be assigned to another array of the same type // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; If the first (10 element) array no longer has a reference to it, it will be garbage collected CS 110: Programming Fundamentals I, Spring 2017 26 12/15/2021

Reassigning Array References An array reference can be assigned to another array of the

Reassigning Array References An array reference can be assigned to another array of the same type // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; The numbers variable holds the address of an int array with 10 elements. Address CS 110: Programming Fundamentals I, Spring 2017 27 12/15/2021

Reassigning Array References An array reference can be assigned to another array of the

Reassigning Array References An array reference can be assigned to another array of the same type // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; The numbers variable holds the address of an int array with 5 elements. This array gets marked for garbage collection Address CS 110: Programming Fundamentals I, Spring 2017 28 12/15/2021

Reassigning Array References An array reference can be assigned to another array of the

Reassigning Array References An array reference can be assigned to another array of the same type // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; The numbers variable holds the address of an int array with 5 elements. The new array's elements are NOT copied over from the first array Address CS 110: Programming Fundamentals I, Spring 2017 29 12/15/2021

Copying arrays You cannot copy an array by merely assigning one reference variable to

Copying arrays You cannot copy an array by merely assigning one reference variable to another You need to copy the individual elements one-by-one from one array to another int[] first. Array = {5, 1, 5, 15, 51}; int[] second. Array = new int[5]; for (int i = 0; i < first. Array. length; i++) second. Array[i] = first. Array[i]; Create an array of 5 integers, whose reference variable is first. Array 5 15 51 What are the initialized values for each element of second. Array? second. Array 0 CS 110: Programming Fundamentals I, Spring 2017 0 0 30 0 0 12/15/2021

Copying arrays You cannot copy an array by merely assigning one reference variable to

Copying arrays You cannot copy an array by merely assigning one reference variable to another You need to copy the individual elements one-by-one from one array to another int[] first. Array = {5, 1, 5, 15, 51 }; int[] second. Array = new int[5]; … and this copies each element from the first to the second array for (int i = 0; i < first. Array. length; i++) second. Array[i] = first. Array[i]; This is the assignment operator …. So whatever the right-hand side evaluates to. . . Is used to update the value at this index position of the second. Array What does this line of code do? . . . walks over the first array, elements by element first. Array second. Array 5 15 51 What are the initialized values for each element of second. Array? 0 CS 110: Programming Fundamentals I, Spring 2017 0 0 31 0 0 12/15/2021

Copying arrays You cannot copy an array by merely assigning one reference variable to

Copying arrays You cannot copy an array by merely assigning one reference variable to another You need to copy the individual elements one-by-one from one array to another int[] first. Array = {5, 1, 5, 15, 51}; int[] second. Array = new int[5]; for (int i = 0; i < first. Array. length; i++) second. Array[i] = first. Array[i]; … and this copies each element from the first to the second array This is the assignment operator …. So whatever the right-hand side evaluates to. . . Is used to update the value at this index position of the second. Array What does this line of code do? . . . first. Array second. Array 5 walks over the first array, elements by element 1 5 15 51 What are the initialized values for each element of second. Array? 0 CS 110: Programming Fundamentals I, Spring 2017 0 0 32 0 0 12/15/2021

Copying arrays You cannot copy an array by merely assigning one reference variable to

Copying arrays You cannot copy an array by merely assigning one reference variable to another You need to copy the individual elements one-by-one from one array to another int[] first. Array = {5, 1, 5, 15, 51 }; int[] second. Array = new int[5]; for (int i = 0; i < first. Array. length; i++) second. Array[i] = first. Array[i]; first. Array second. Array 5 15 51 05 1 0 05 15 0 51 0 When i = 1 When i = 2 When i = 3 When i = 4 Value of first. Array[0] is used to update second. Array[0] Value of first. Array[1] is used to update second. Array[1] Value of first. Array[2] is used to update second. Array[2] Value of first. Array[3] is used to update second. Array[3] Value of first. Array[4] is used to update second. Array[4] CS 110: Programming Fundamentals I, Spring 2017 33

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[]

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[] args){ 3 4 int[] array. Of. Ints = {56, 45, 34}; 5 int[] duplicate. Ints = new int[6]; 6 7 int array. Index = 0; 8 for (int array. Element : array. Of. Ints){ 9 duplicate. Ints[array. Index] = array. Element; 10 duplicate. Ints[array. Index+2] = array. Element; 11 array. Index++; 12 }}} After the above java code is executed, what are the contents of the elements in the duplicate. Ints array? A. B. C. D. E. 56 45 34 0 0 0 58 47 36 56 45 34 0 CS 110: Programming Fundamentals I, Spring 2017 34 12/15/2021

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[]

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[] args){ 3 4 int[] array. Of. Ints = {56, 45, 34}; 5 int[] duplicate. Ints = new int[6]; 6 7 int array. Index = 0; 8 for (int array. Element : array. Of. Ints){ 9 duplicate. Ints[array. Index] = array. Element; 10 duplicate. Ints[array. Index+2] = array. Element; 11 array. Index++; 12 }}} After this line is executed … the array duplicate. Ints is created, and initialized array. Of. Ints 56 45 34 After this line is executed … the variable array. Index of type int is declared and set to 0 array. Index = 0 0 CS 110: Programming Fundamentals I, Spring 2017 0 0 35 0 0 0 12/15/2021

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[]

Processing/Accessing Array Contents 1 public class copying. Arrays { 2 public static void main(String[] args){ 3 4 int[] array. Of. Ints = {56, 45, 34}; 5 int[] duplicate. Ints = new int[6]; 6 7 int array. Index = 0; 8 for (int array. Element : array. Of. Ints){ 9 duplicate. Ints[array. Index] = array. Element; 10 duplicate. Ints[array. Index+2] = array. Element; 11 array. Index++; 12 }}} array. Of. Ints array. Index = 0 56 duplicate. Ints 0 56 CS 110: Programming Fundamentals I, Spring 2017 array. Index = 1 45 array. Index = 2 34 0 45 0 34 56 36 0 45 0 34 0 12/15/2021

Up Next. . . • Passing Arrays as arguments • Returning Arrays from methods

Up Next. . . • Passing Arrays as arguments • Returning Arrays from methods • String Arrays CS 110: Programming Fundamentals I, Spring 2017 37 12/15/2021