1 / 12

MIS 222 – Lecture 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;.

chi
Download Presentation

MIS 222 – Lecture 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIS 222 – Lecture 12 10/9/2003

  2. Assignment 11 • Demo • 8.1.10

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

  4. int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; enclosed in brackets separated by commas String[] letters = {“a”, “b”, “c”}; System.out.println(letters[2]); System.out.println(letters[3]); Array Literals

  5. Array Literals Example • 8.2.1

  6. Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; int[] myComps = companies; myComps[0] = 1; System.out.println(companies[0]); companies 8475 Shallow Copy myComps 8475 0 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 8475

  7. Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; int[] myComps =new int[companies.length]; System.arraycopy(companies, 0, myComps, 0, companies.length); myComps[0] = 1; System.out.println(companies[0]); companies 8475 Deep Copy 0 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 8475 myComps 9985 0 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 9985

  8. companies 8475 0 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 8475 Arrays as Arguments (to methods) Average() int[] int 25

  9. 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; } }

  10. 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] = -999999999; return total / num.length; } }

  11. 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){ //body same as previous slide } } stuff 8475 nums 0 1 2 3 8475 5 15.5 55.55 1 8475

  12. Example • 8.3.7

More Related