Java Array Lists 1292019 Goals Understand how to

  • Slides: 14
Download presentation
Java Array Lists 12/9/2019

Java Array Lists 12/9/2019

Goals • Understand how to create and use an Array. List • Become familiar

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

What do you think the following code will do? Scanner input = new Scanner(System.

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

Array List • A dynamic array of objects • Like a linked list. You

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 Array. Lists!!!! (But we will focus on those in the AP subset)

Objects only. • A disadvantage of a Array. List is that it holds only

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

Automatic expansion. • Use Array. List when there will be a large variation in

Automatic expansion. • Use Array. List 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 email contact list because there is no upper bound on the number of contacts.

Creating a Array. List Import java. util. Array. List; //Before the main body Array.

Creating a Array. List Import java. util. Array. List; //Before the main body Array. List<Some. Class> list. Name = new Array. List<Some. Class>(); Array. List<String> list = new Array. List<String>(); Array. List<Integer> num = new Array. List<Integer>();

AP Subset Array. List Methods • x=alist. size(); Returns the number of elements in

AP Subset Array. List Methods • x=alist. size(); Returns the number of elements in the Array. List. • alist. add(obj); Adds an object onto the end of the Array. List, 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. • obj. Ans = alist. get(index); This returns the element at the index position. • obj. Ans = alist. set(index, obj); Assigns the object, obj, to position N in the Array. List, replacing the item previously stored in position N. It also returns the element at index. • obj. Ans = alist. remove(N); For an integer, N, this removes the N-th item in the Array. List. 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.

Dry Run Problem CTA 18 Consider the following instance variable and method from some

Dry Run Problem CTA 18 Consider the following instance variable and method from some class. private Array. List<Integer> values; public void change. List(int limit) { for(int k = 0; k < values. size(); k++) { if(values. get(k). int. Value() < limit) values. add(values. remove(k)); } } . int. Value() returns the int value of an Integer object.

Review Array. List<Some. Class> list. Name = new Array. List<Some. Class>(); Array. List<String> list

Review Array. List<Some. Class> list. Name = new Array. List<Some. Class>(); Array. List<String> list = new Array. List<String>(); Array. List<Integer> num = new Array. List<Integer>(); x=alist. size(); alist. add(obj); alist. add(index, obj); obj. Ans = alist. get(index); obj. Ans = alist. set(index, obj); obj. Ans = alist. remove(N);

Warm up • Look up Java Docs to find out what the following Integer

Warm up • Look up Java Docs to find out what the following Integer methods do… • Constructors • . int. Value() • . compare. To() • . equals()

Sample Question • 2. Consider the following code segment. – – – – Array.

Sample Question • 2. Consider the following code segment. – – – – Array. List <Integer> list = new Array. List<Integer> (); list. add(new Integer(1)); Note: This will display the entire list. add(new Integer(2)); arraylist!! 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]

 • 3. Consider the following data field and method. – private Array. List

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

Array. List Program #1 • Input: An unknown number of names. – Output: The

Array. List Program #1 • Input: An unknown number of names. – Output: The names in reverse order • Create a Person class – Name, year. Of. Birth – Constructors (Default and with values), to. String, methods access. Name() and access. Year() – Program: Input an unknown number of Person’s, output the name of the Person that is youngest.