Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Array Lists 2/13/2017.

Similar presentations


Presentation on theme: "Java Array Lists 2/13/2017."— Presentation transcript:

1 Java Array Lists 2/13/2017

2 Goals Understand how to create and use an ArrayList
Become familiar with the AP Java subset of ArrayList methods Be able to write a program using an ArrayList.

3 What do you think the following code will do?
Scanner input = new Scanner(System.in); ArrayList <String> names = new ArrayList<String>(); String name; System.out.println("Enter a name"); name = input.next(); while (!name.equals("-1")) { names.add(name); } for(int count = 0; count<names.size();count++) System.out.println(names.get(count));

4 Array List A dynamic array of objects
Like a linked list. You can dynamically add to it, remove from it,… You can also have random(ish) access. You can add, get, set, and remove an object using methods (You don’t have to write the code for these!) There are many other methods tied to ArrayLists!!!! (But we will focus on those in the AP subset)

5 Objects only. A disadvantage of a ArrayList is that it holds only objects and not primitive types (i.e. int). To use a primitive type in an ArrayList, put it inside an object (i.e. to save an integer value use the Integer ‘wrapper’ class or define your own class).

6 Automatic expansion. Use ArrayList when there will be a large variation in the amount of data that you would put into an array. Arrays should be used only when there is a constant amount of data. For example, storing information about the days of the week should use an array because the number of days in a week is constant. Use an array list for your contact list because there is no upper bound on the number of contacts.

7 Creating a ArrayList Import java.util.ArrayList; //Before the main body ArrayList<SomeClass> listName = new ArrayList<SomeClass>(); ArrayList<String> list = new ArrayList<String>(); ArrayList<Integer> num = new ArrayList<Integer>();

8 AP Subset ArrayList Methods
x=alist.size(); Returns the number of elements in the ArrayList. alist.add(obj); Adds an object onto the end of the ArrayList, increasing the size by 1. alist.add(index, obj); Inserts obj at position index (0<= index < size) moving elements at position index and higher to the right. Also adjusts the alist’s size. objAns = alist.get(index); This returns the element at the index position. objAns = alist.set(index, obj); Assigns the object, obj, to position N in the ArrayList, replacing the item previously stored in position N. It also returns the element at index. objAns = alist.remove(N); For an integer, N, this removes the N-th item in the ArrayList. N must be in the range 0 to alist.size()-1. Any items in the list that come after the removed item are moved down one position.

9 .intValue() returns the int value of an Integer object.
Dry Run Problem CTA18 Consider the following instance variable and method from some class. private ArrayList<Integer> values; public void changeList(int limit) { for(int k = 0; k < values.size(); k++) if(values.get(k).intValue() < limit) values.add(values.remove(k)); } .intValue() returns the int value of an Integer object.

10 ArrayList vs. array usage
import java.util.*; construction String[] names = new String[5]; ArrayList <String> namesList = new ArrayList() <String> ; storing an element names[0] = "Jennifer"; namesList.add("Jennifer"); // or, namesList.add(0, "Jennifer"); retrieval of an element's value String name = names[0]; String name = namesList.get(0);

11 ArrayList vs. array, cont'd.
removal of an element (at index 2) for (int i = 2; i < names.length - 1; i++) names[i] = names[i+1];//Array namesList.remove(2);//ArrayList

12 Review ArrayList<SomeClass> listName = new ArrayList<SomeClass>(); ArrayList<String> list = new ArrayList<String>(); ArrayList<Integer> num = new ArrayList<Integer>(); x=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);

13 Warm up Look up Java Docs to find out what the following Integer methods do… Constructors .intValue() .compareTo() .equals()

14 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!!

15 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]

16 Inheritance Review Write the code to create the following classes. Use ‘Is a’ and ‘Has a…’ to determine inheritance and instance variables. Person class Name, yearOfBirth Constructors (Default and with values), toString, methods accessName() and accessYear()

17 ArrayList Program #1 Input: An unknown number of names.
Output: The names in reverse order Create a Person class Name, yearOfBirth Constructors (Default and with values), toString, methods accessName() and accessYear() Program: Input an unknown number of Person’s, output the name of the Person that is youngest.


Download ppt "Java Array Lists 2/13/2017."

Similar presentations


Ads by Google