MIS 222 Lecture 12 1092003 Assignment 11 Demo


![Arrays int[] companies = new int[8]; companies[0] = 4000; companies[1] = 122154; companies[2] = Arrays int[] companies = new int[8]; companies[0] = 4000; companies[1] = 122154; companies[2] =](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-3.jpg)
![Array Literals int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; enclosed in Array Literals int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; enclosed in](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-4.jpg)

![Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my. Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my.](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-6.jpg)
![Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my. Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my.](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-7.jpg)
![Arrays as Arguments (to methods) Average() int[] int companies 8475 0 1 2 3 Arrays as Arguments (to methods) Average() int[] int companies 8475 0 1 2 3](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-8.jpg)

![Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-10.jpg)
![Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff](https://slidetodoc.com/presentation_image_h/2d19ff0755ff5049cd6b206f166316ce/image-11.jpg)

- Slides: 12
MIS 222 – Lecture 12 10/9/2003
Assignment 11 ► Demo § 8. 1. 10
Arrays int[] companies = new int[8]; companies[0] = 4000; companies[1] = 122154; companies[2] = 55255; companies[3] = -564654; companies[4] = 541818; companies[5] = 781849; companies[6] = 121518; companies[7] = 121518; Exact same data types ► Similar information ► Company Profits 1 2 3 4 5 6 7 8 4000 122154 55255 -564654 541818 781849 -121518 843257
Array Literals int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; enclosed in brackets separated by commas String[] letters = {“a”, “b”, “c”}; System. out. println(letters[2]); System. out. println(letters[3]);
Array Literals Example ► 8. 2. 1
Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my. Comps = companies; my. Comps[0] = 1; System. out. println(companies[0]); companies 8475 my. Comps w o l l a h S 8475 0 1 4000 8475 122154 2 55255 3 y p o C 4 -564654 541818 5 6 7 781849 -121518 843257
Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518}; int[] my. Comps =new int[companies. length]; System. arraycopy(companies, 0, my. Comps, 0, companies. length); my. Comps[0] = 1; System. out. println(companies[0]); companies 8475 0 1 4000 8475 my. Comps 9985 0 4000 9985 2 3 p e e D 122154 55255 1 2 122154 55255 y p o C 4 -564654 541818 3 4 -564654 541818 5 6 7 781849 -121518 843257
Arrays as Arguments (to methods) Average() int[] int companies 8475 0 1 2 3 4 4000 8475 122154 55255 -564654 541818 5 6 7 781849 -121518 843257 25
Arrays as Arguments Here’s what it looks like: public class Average { public static void main(String[] args){ double[] stuff = {5, 15. 5, 55. 55, 1}; double avg = average(stuff); System. out. println(“average of stuff is “ + avg); } } public static double average(double[] nums){ double total = 0; for(int i = 0; i < nums. length; i++){ total += nums[i]; } return total / num. length; }
Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff = {5, 15. 5, 55. 55, 1}; double avg = average(stuff); System. out. println(“the third element is “ + stuff[2]); } } public static double average(double[] nums){ double total = 0; for(int i = 0; i < nums. length; i++){ total += nums[i]; } //malicious code nums[2] = -99999; return total / num. length; }
Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff = {5, 15. 5, 55. 55, 1}; double avg = average(stuff); System. out. println(“the third element is “ + stuff[2]); } stuff } public static double average(double[] nums){ //body same as previous slide } 8475 nums 8475 0 1 5 8475 2 15. 5 55. 55 3 1
Example ► 8. 3. 7