Download presentation
Presentation is loading. Please wait.
1
MIS 222 – Lecture 12 10/9/2003
2
Assignment 11 Demo 8.1.10
3
Arrays Company Profits 1 4000 2 122154 3 55255 4 -564654 5 541818 6
int[] companies = new int[8]; companies[0] = 4000; companies[1] = ; companies[2] = 55255; companies[3] = ; companies[4] = ; companies[5] = ; companies[6] = ; companies[7] = ; Company Profits 1 4000 2 122154 3 55255 4 5 541818 6 781849 7 8 843257 Exact same data types Similar information
4
Array Literals int[] companies = {4000, , 55255, , , , , }; enclosed in brackets separated by commas String[] letters = {“a”, “b”, “c”}; System.out.println(letters[2]); System.out.println(letters[3]);
5
Array Literals Example
8.2.1
6
Shallow Copy Array Copying
int[] companies = {4000, , 55255, , , , , }; int[] myComps = companies; myComps[0] = 1; System.out.println(companies[0]); companies 8475 Shallow Copy myComps 8475 1 2 3 4 5 6 7 4000 122154 55255 541818 781849 843257 8475
7
Deep Copy Array Copying
int[] companies = {4000, , 55255, , , , , }; 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 1 2 3 4 5 6 7 4000 122154 55255 541818 781849 843257 8475 myComps 9985 1 2 3 4 5 6 7 4000 122154 55255 541818 781849 843257 9985
8
Arrays as Arguments (to methods)
Average() int[] int 8475 companies 4000 122154 55255 541818 781849 843257 1 2 3 4 5 6 7 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] = ; 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 1 2 3 8475 5 15.5 55.55 1 8475
12
Example 8.3.7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.