Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Array Lists Day 2.

Similar presentations


Presentation on theme: "Java Array Lists Day 2."— Presentation transcript:

1 Java Array Lists Day 2

2 Goals Review the AP Java subset of ArrayList methods
Create a resource in your notes for Integer class methods and constants that are part of the AP subset. Be able to read und solve Multiple Choice type questions that use ArrayLists. Be able to write a second ArrayList program

3 Array List: Review Creating an ArrayList A dynamic array of objects.
Need to use Wrapper classes for primitives Creating an ArrayList ArrayList<SomeClass> listName = new ArrayList<SomeClass>(); ArrayList<String> list = new ArrayList<String>(); ArrayList<Integer> num = new ArrayList<Integer>(); ArrayList methods integerVariable = alist.size(); alist.add(obj); alist.add(index, obj); objAns = alist.get(index); objAns = alist.set(index, obj); alist.set(index, obj); objAns = alist.remove(N); alist.remove(N);

4 Warm up Use JavaDocs to look up the following for the Integer class and add these to your notes. Constructors .intValue() .compareTo() .equals() .MIN_VALUE .MAX_VALUE

5 What does this code do?

6 Note: This will display the entire arraylist!!
Sample Question 2. Consider the following code segment. ArrayList <Integer> list = new ArrayList<Integer> (); list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.set(2, new Integer(4)); list.add(2, new Integer(5)); list.add(new Integer(6)); System.out.println(list); What is printed as a result of executing the code segment? (A) [1, 2, 3, 4, 5] (B) [1, 2, 4, 5, 6] (C) [1, 2, 5, 4, 6] (D) [1, 5, 2, 4, 6] (E) [1, 5, 4, 3, 6] Note: This will display the entire arraylist!!

7 3. Consider the following data field and method.
private ArrayList nums; // precondition: nums.size() > 0; // nums contains Integer objects public void numQuest() { int k = 0; Integer zero = new Integer(0); while (k< nums.size()) if (nums.get(k).equals(zero)) nums.remove(k); k++; } Assume that ArrayList nums initially contains the following Integer values. [0, 0, 4, 2, 5, 0, 3, 0] What will ArrayList nums contain as a result of executing numQuest ? (A) [0, 0, 4, 2, 5, 0, 3, 0] (B) [4, 2, 5, 3] (C) [0, 0, 0, 0, 4, 2, 5, 3] (D) [3, 5, 2, 4, 0, 0, 0, 0] (E) [0, 4, 2, 5, 3]

8 ArrayList Program #2 Just Above Average Random Compliment Generator
Input 10 integers. Output: The total, average and number of scores above the average of these scores. Push: Create a Person class with name and score and show the names of above average people. Random Compliment Generator Initialize: Code 5 compliments stored in an arrayList Process: Ask the user if they would like a compliment. They can ask for compliments as long as they would like. (Semantics of a while loop) Output: The computer will randomly pick and show a compliment. Phone(y) Book Input: An unknown number of names Process: Add the names to the list so the list remains in order. Output: Show all the names Push: Create a Contact class that includes phone, name, and and use an arrayList of Contacts.


Download ppt "Java Array Lists Day 2."

Similar presentations


Ads by Google