Title: CS1101: Programming Methodology Recitation 5
1CS1101 Programming MethodologyRecitation 5
Arrays and Collections
2Array Basics
- Index of first position in an array is 0.
- Length value of an array
- An array is a reference data type
- double A new double12 //array of double
- int size A.length
- Do not mix up with length method of a String
object - String s This is a string
- int size s.length()
- Array of objects
3Constructors for Person class
- public class Person
- private int age
- private char gender
- private String name
- public Person()
- // Default age is 0
- // Gender of a person F for female, M for male,
U for unknown. - // Default name is set to NotGiven.
- this (0, U, Not Given)
-
- public Person(int age, char gender, String name)
- this.setAge (age)
- this.setGender (gender)
- this.setName (name)
-
4Public Methods of Person class
- public void setAge (int personAge)
- age personAge
-
- public void setGender (char personGender)
- gender personGender
-
- public void setName (String personName)
- name personName
-
- public int getAge ()
- return age
-
- public char getGender ()
- return gender
-
- public String getName ()
- return name
-
5- Person latte
- latte new Person()
- latte.setName(Ms. Latte)
- latte.setAge(20)
- latte.setGender(F)
- System.out.println (Name latte.getName()
) - System.out.println (Age latte.getAge()
) - System.out.println (Sex
latte.getGender() )
6- Create and manipulate an array of Person objects
- Person person //declare person array
- person new Person20 //and then create it
- Note The elements, Person objects, are not yet
created. - person0 new Person()
- person0.setName (Ms. Latte)
- person0.setAge (20)
- person0.setGender (F)
null
Person0 new Person (20, F, Ms. Latte)
7Set up person array
- String name
- int age
- char gender
- for (int i0 i lt person.length i)
- System.out.println (Enter name )
- name stdin.readLine()
-
- System.out.println (Enter age )
- age Integer.parseInt(stdin.readLine())
- System.out.println (Enter gender )
- gender stdin.readLine().charAt(0)
- personi new Person()
- personi.setName(name)
- personi.setAge(age)
- personi.setGender(gender)
8Find average age
- double sum 0 aveAge
- for (int i0 i lt person.length i)
- sum personi.getAge()
-
- aveAge sum / person.length
9Find youngest and oldest persons
- int minIdx, maxIdx //Index to youngest and
oldest persons - minIdx maxIdx 0
- for (int i0 i lt person.length i)
- if (personi.getAge() lt personminIdx.getAge())
- //found a younger person
- minIdx i
-
- else if (personi.getAge() gt personmaxIdx.getA
ge()) - //found an older person
- maxIdx i
-
-
- System.out.println (Oldest
personmaxIdx.getName() is
personmaxIdx.getAge() years old.) - System.out.println (Youngestt
personminIdx.getName() is
personminIdx.getAge() years old.)
10Find youngest and oldest persons - an array of
Person objects and two Person var
- Person youngest, oldest //Points to youngest and
oldest persons - youngest oldest person0
- for (int i0 I lt person.length i)
- if (personi.getAge() lt youngest.getAge())
- youngest personi
-
- else if (personi.getAge() gt oldest.getAge())
- oldest personi
-
-
- System.out.println (Oldest oldest.getName()
is oldest.getAge() years old.) - System.out.println (Youngestt
youngest.getName() is youngest.getAge()
years old.)
11An array of Person objects and two Person
variables
0 1 2 3
17 18 19
person
Person Jane 87 F
Person Ms. Latte 20 F
oldest
youngest
12Find a particular person
- int i 0
- while (i lt person.length //still more persons
to search - !personi.getName().equals(Latte))
- i
-
- if (i person.length) //unsuccessful search
- System.out.println (Ms. Latte was not in the
array) -
- else //found successful search
- System.out.println (Found Ms. Latte at position
i)
13Delete a particular person
- Two approaches
- Reset array element to null.
- Leave holes in array
- Pack elements so that real references occur at
the beginning and null references at the end - If object at position j is removed, then elements
from position j1 up to last non-null reference
are shifted one position lower - Order of Person objects is maintained.
- Replace removed element by last element in the
array
14 0 1 2
3
person
Person B
Person C
Person A
Person D
int delIdx 1 persondelIdx null
0 1 2
3
person
Person C
Person A
Person D
15 0 1 2
3
person
Person B
Person C
Person A
Person D
int delIdx 1 int last 3 persondelIdx
personlast personlast null
0 1 2
3
person
Person C
Person A
Person D
16Complete program Illustrate processing of an
array of Person objects
- public class testPerson
- public static void main(String args)
- Person person //declare person array
- person new Person5 //and then create it
- //--------------create person
array-----------------------// - String name
- int age
- char gender
-
- for (int i0 i lt person.length i)
- System.out.println (Enter name )
- name stdin.readLine()
- System.out.println (Enter age )
- age Integer.parseInt(stdin.readLine())
- System.out.println (Enter gender )
- gender stdin.readLine().charAt(0)
17- //--------------compute average
age----------------------// - float sum 0 aveAge
- for (int i0 i lt person.length i)
- sum personi.getAge()
-
- aveAge sum / (float) person.length
- System.out.println (Average age aveAge)
- //--------------find oldest and youngest
person----------// - Person youngest, oldest //Points to youngest
and oldest persons -
- youngest oldest person0
- for (int i0 I lt person.length i)
- if (personi.getAge() lt youngest.getAge())
- youngest personi
-
18- //--------------search for a particular
person---------------------// - System.out.println (Name to search )
- String searchName stdin.readLine()
- int i 0
- while (i lt person.length //still more persons
to search - !personi.getName().equals(searchName))
- i
-
- if (i person.length) //unsuccessful search
- System.out.println (searchName was not in
the array) -
- else //found successful search
- System.out.println (Found searchName at
position i) -