Questions? Email sfo.lax6776@gmail.com

Join our weChat group or follow us on Facebook

©2019 by Legion of Learners

sfolax6776
Mar 8

11.4 How to create an array list of objects?

0 comments

Edited: Oct 14

A. What do array lists and arrays have in common? - not much

B. Is ArrayList a class or an interface? - a class that implements List interface

C. What is <E>? - a type parameter to specify what type of objects will the array list have.

Arrays vs ArrayLists

D. How to use for/for each loops to traverse an array list?

E. How to create an Iterator object for an array list?

F. How to use the Iterator object to traverse an array list? - hasNext(), next(), remove()

G. An example: Q2 of 2018 AP CSA FRQ

H. Practice of the week:

  1. All array list questions of Chapter 7 "Arrays and Array Lists" (Barron's)

  2. Finish up Q2 of 2018 AP CSA FRQ

 

New Posts
  • sfolax6776
    Oct 28

    A. Introduction Anything is possible if you can program... :) B. Watch (Slides) C. Try: 1) Do you know ArrayList class is implemented by an array? 2) Can you implement insert/remove for 2-D arrays?
  • sfolax6776
    Oct 28

    A. Introduction Arrays are powerful but their sizes are fixed once declared. To overcome the inconvenience, array lists are introduced in Java. Array lists are implemented by arrays behind the scene but to end users, they are resizable. B. Watch ( Slides ) C. Try: E.7.11-E7.14 of Big Java Early Obejcts
  • sfolax6776
    Oct 28

    A. Introduction Now you are well equipped with the skills to create an array of 300 students, - not only their height or weight, but ALL info of these students. You define what you'd like to include in class Students, and then create an array of 300 Student objects. Each object may have a student's name, birthday, height, weight, gpa... B. Watch ( slides ) C. Try 1) Use class Person below to create an array of x persons. Prompt a user to enter x as numbers of persons to create, then input name and age of each person to fill the array. public class Person { private String name; private int age; public Person(String aName, int anAge) { name = aName; age = anAge; } /* @return the String form of this person / public String toString() { return name + " " + age; } public void printPerson() { System.out.println(this); } public void setName(String newName) { name = newName; } public void setAge(int newAge) { age = newAge; } } 2) Split the array above into two or more age groups, create a new array for each age group. 3) Create a 2-D array of Person to store different age groups: <12 years old, 12-18 years old, >18 years old. Assume each age group will have a maximum of 5 people.