Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 116/504 – Intro. To Computer Science for Majors II

Similar presentations


Presentation on theme: "CSE 116/504 – Intro. To Computer Science for Majors II"— Presentation transcript:

1 CSE 116/504 – Intro. To Computer Science for Majors II
Lecture 9: Arrays

2 Announcements Grades posted to UBLearns on regular basis
May take few days, but allows you to track progress Lowest 3 homeworks dropped to calculate final grade Will also see lowest quiz grade excluded in calculations Homework for week due Mondays at 12:45PM Problems NOT optional If it takes longer than 15 minutes, get help ASAP

3 Inheritance Example public class SuperClass { public String getMyString() { return "SUPER"; } } public class SubClass extends SuperClass { public String getMyString() { return "sub " + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub2, sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); SubClass sub2 = (SubClass)super1;

4 Declaring Array Variables
Like all variables, must declare before use Type, brackets, & name declare array variable Still variables, so names follow usual rules Variable is array of the type, so use any legal type Each of the array's entries hold value of that type String[] bob; long[] johnSilver; VWCar[] errorProne;

5 Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar];
Instantiating Arrays Cannot use until after instantiating array Type & length of array needed for instantiation Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Type is for elements & must be assignable to array’s type Size must be int since ½ a value hard to use

6 Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar];
Instantiating Arrays Cannot use until after instantiating array Type & length of array needed for instantiation Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Type is for elements & must be assignable to array’s type Size must be int since ½ a value hard to use

7 Array Basics #1 Must specify size when allocated: Cannot grow array Cannot shrink array

8 Which Declaration is NOT Legal?
int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; 27

9 Which Declaration is NOT Legal?
int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Convince your neighbor your answer is correct

10 Which Declaration is NOT Legal?
int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; 4

11 Which Declaration is NOT Legal?
int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Need to use new to create array instances.

12 Which Declaration is NOT Legal?
int[] iBob = new int[23]; double[] dBob; Float fBob[17]; Float[] fBob = new Float[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Need to use new to create array instances.

13 Assigning Array Variables
Can be set to null to mark there is no array Can be set to newly instantiated array Alias array by assigning to another array variable Does not copy array, but has both refer to same object Change to entry seen by both variables, since just aliases TV image is public domain: Remote images by Dwight Burdette and used under Creative Commons 3.0 License. Original is at:

14 Array Key Concept #2 Array variable is reference variable target either array instance or null

15 Memory Trace Example int[] data = new int[3]; int[] al = data; for (int i = 0; i < data.length; i++) { data[i] = i * 2; } al[0] = 5; al = new int[1]; al[0] = 7;

16 Array Key Concept #3 Allocating array of object type DOES NOT allocate other objects for its entries

17 Entries Are Variables, Too!
Within an array entries like individual variables int[] bob = new int[30]; Car[] parkingLot = new Car[300]; bob’s entries would be primitive variables parkingLot’s entries refer to instances Entries initialized like variable when array created bob’s entries would all be set to 0 null stored in all entries in parkingLot

18 Array entries act like fields Should initialize before using
Array Key Concept #4 Array entries act like fields Should initialize before using

19 Entries Are Variables, Too!
Object[] data = new Object[3]; Object[] al = data; for (int i = 0; i < data.length; i++) { data[i] = new Object(); } al[0] = new Object(); al = new Object[1]; al[0] = data[0]; data[0] = data[1];

20 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; 26

21 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Convince your neighbor your answer is correct

22 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

23 Which is Correct Java Code?
In new, size of array written in brackets int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

24 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Must assign array entries individually

25 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Entries at indices 0 - length - 1

26 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Specify entry by writing index in brackets

27 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Entry like variable of array's type

28 Which is Correct Java Code?
int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

29 Array Basics #1 Must specify size when allocated: Cannot grow array Cannot shrink array

30 Array Key Concept #2 Array variable is reference variable target either array instance or null

31 Array Key Concept #3 Allocating array of object type DOES NOT allocate other objects for its entries

32 Array entries act like fields Should initialize before using
Array Key Concept #4 Array entries act like fields Should initialize before using

33 For Next Lecture Discussing 1st (& most interesting) ADT Wednesday
What is an ADT and why do ADTs drive the world? What is a MultiSet & why only in Google’s Java API? How does MultiSet differ from array or ArrayList? How to use MultiSet in our programs? There are week #4 homework problems on web Due by 12:45PM Monday for this week & all following


Download ppt "CSE 116/504 – Intro. To Computer Science for Majors II"

Similar presentations


Ads by Google