Array Lists 7.7.

Slides:



Advertisements
Similar presentations
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Advertisements

Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
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.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 6 – Array and Array Lists Two-Dimensional Arrays
slides created by Marty Stepp
Chapter 7 – Arrays and Array Lists
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Array Lists An array list stores a sequence of values whose size can change. An array list can grow and shrink as needed. ArrayList class supplies methods.
Collections.
Collections.
Ch7. List and Iterator ADTs
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CSC 222: Object-Oriented Programming Fall 2017
A tree set Our SearchTree class is essentially a set.
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Arrays in C.
Detailed File I/O Examples Focus on Input
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Welcome to CSE 143!.
Array List Pepper.
Building Java Programs
CS 106A, Lecture 19 ArrayLists
Java for Beginners University Greenwich Computing At School DASCO
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
ARRAYS & ARRAY LISTS.
CSE 143 Lecture 4 ArrayList Reading: 10.1.
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.
Chapter 8 Slides from GaddisText
" A list is only as strong as its weakest link. " - Donald Knuth
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
A tree set Our SearchTree class is essentially a set.
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Building Java Programs
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Object Oriented Programming in java
Building Java Programs
Topics discussed in this section:
Methods Copying Autoboxing
Arrays ICS2O.
slides created by Ethan Apter
CSC 221: Computer Programming I Fall 2009
Array Lists CSE 1310 – Introduction to Computers and Programming
Lecture 13: ArrayLists AP Computer Science Principles
Programming Control Structures with JavaScript Part 2
CSE 143 Lecture 2 More ArrayList; classes and objects
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Marty Stepp
Building Java Programs
Dr. Sampath Jayarathna Cal Poly Pomona
slides created by Marty Stepp
Lecture 6: References and linked nodes reading: 16.1
ArrayLists Readings: 10.1 (pg. 559 – 571).
Arrays and ArrayLists.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Presentation transcript:

Array Lists 7.7

Array Lists An array list acts much like an array with a few exceptions. It is actually an object, so it has methods which can be called Its size is variable and can change as necessary Array lists have size, not length. size( ) Only stores objects To access an element you use the get(i) method The add( someObject ) method adds an object to the end of the list.

Array Lists set(i, object) will change what is stored at index i to whatever object is passed remove(i) will remove the object at index i and will adjust the size of the array

Syntax ArrayList<type> variable = new ArrayList<type>(); ArrayList<String> names = new ArrayList<String>(); ArrayList<Dice> dice = new ArrayList<Dice>(); ArrayList<int> values = new ArrayList<int>(); ArrayList<double> values = new ArrayList<double>();

ArrayList<String> names = new ArrayList<String>(); names.add("Emily"); // Now names has size 1 and element "Emily" names.add("Bob"); // Now names has size 2 and elements "Emily", "Bob" names.add("Cindy"); // names has size 3 and elements "Emily", "Bob", and "Cindy"

Examples

Homework Download the file GettysburgAddress.txt from the website. You are going to use a scanner to read each work of the Gettysburg address and add each word to an arraylist of Strings. The program should then identify the longest word in the address, as well as the average word length. Scanner file = new Scanner(new File(filename)); // assume filename stores the name of the file while (file.hasNext()) { word = file.next(); }