Java Array Lists 2/13/2017.

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 12 The ArrayList Data Structure Section 1 - How to Instantiate an ArrayList Section 2 - The ArrayList Subset Section 3 - Declaring an ArrayList.
The ArrayList Data Structure The Most Important Things to Review.
The ArrayList Data Structure Standard Arrays at High Speed!
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
CMSC 202 ArrayList Aug 9, 2007.
slides created by Marty Stepp
Chapter 7 – Arrays and Array Lists
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 20: Wrapper Classes and More Loops
Collections.
Collections.
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Array List Pepper.
Building Java Programs
Java Array Lists 2/2/2016.
CS 106A, Lecture 19 ArrayLists
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
Building Java Programs Chapter 10
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
CS 200 Objects and ArrayList
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
int [] scores = new int [10];
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
ArrayLists.
CSE 143 Lecture 2 Collections and ArrayIntList
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Lecture 1: ArrayList reading: 10.1
Dynamic Data Structures and Generics
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
slides created by Ethan Apter
Array Lists CSE 1310 – Introduction to Computers and Programming
Java Array Lists 2/13/2017.
Lecture 13: ArrayLists AP Computer Science Principles
ArrayLists 22-Feb-19.
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
CS 200 Objects and ArrayList
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Dynamic Data Structures and Generics
Review of Previous Lesson
Java Array Lists Day 2.
ArrayLists.
Building Java Programs
ArrayList Day 3 Be able to read AP Mult Choice questions tied to ArrayLists Be able to write a program that uses ArrayLists that analyzes numbers.
Review: libraries and packages
Java: Variables, Input and Arrays
slides created by Marty Stepp
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CS 200 Objects and ArrayList
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

Java Array Lists 2/13/2017

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.

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));

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)

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).

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 email contact list because there is no upper bound on the number of contacts.

Creating a ArrayList ArrayList<SomeClass> listName = new ArrayList<SomeClass>(); ArrayList<String> list = new ArrayList<String>(); ArrayList<Integer> num = new ArrayList<Integer>();

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.

.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.

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);

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

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);

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

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

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]

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()

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.