The person class r We will use Person objects to illustrate the use of an array of objects. Person latter The Person class supports the set methods latte new Person()i and get methods latte setName(" Ms. Latte)i latte. setAge(20)i latte. set Gender(F)i outputBox. printLine(Name:+ latte getName() outputBox. printLine("Age : latte getAge( outputBox. printline(Sex : latte getGender ( C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-11
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 11 The Person Class We will use Person objects to illustrate the use of an array of objects. Person latte; latte = new Person( ); latte.setName("Ms. Latte"); latte.setAge(20); latte.setGender('F'); outputBox.printLine( "Name: " + latte.getName() ); outputBox.printLine( "Age : " + latte.getAge() ); outputBox.printLine( "Sex : " + latte.getGender() ); The Personclass supports the set methods and get methods
Creating an array of person objects -1 Code (A[p Person[ personi person new Person[20]i Only the name person is declared, no array Is person [o] = new Person( )i allocated yet oerson State of Memory After(A) is executed C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-12
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 12 Creating an Array of Person Objects - 1 Code State of Memory Person[ ] person; person = new Person[20]; person[0] = new Person( ); A Only the name person is declared, no array is allocated yet. After is executed A person
Creating an array of person objects -2 Code Person[ personi Now the array for storing B person= new Person[201; 20 Person objects is created. but the person person [o] = new Person( )i objects themselves are not yet created oerson 01234 16171819 State of Memory After(B) is executed C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-13
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 13 person Creating an Array of Person Objects - 2 Code State of Memory Person[ ] person; person = new Person[20]; person[0] = new Person( ); B Now the array for storing 20 Person objects is created, but the Person objects themselves are not yet created. After is executed B 0 1 2 3 4 16 17 18 19 person
Creating an array of person objects -3 Code Person[ personi One Person object is person new Person [20] created and the reference to this object is placed in person [o] = new Person( )i position O oerson 01234 16171819 State of Person Memory After(C) is executed C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 9-14
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 9 - 14 Creating an Array of Person Objects - 3 Code State of Memory Person[ ] person; person = new Person[20]; C person[0] = new Person( ); One Personobject is created and the reference to this object is placed in position 0. 0 1 2 3 4 16 17 18 19 person 0 1 2 3 4 16 17 18 19 person After is executed C Person