Methods Copying Autoboxing

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

© A+ Computer Science - ArrayList © A+ Computer Science - Lab 16.
Building Java Programs
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
The ArrayList Data Structure The Most Important Things to Review.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Elevens is a lab about classes and Lists. List is a major concept being tested by the Elevens lab. Elevens.
EKT472: Object Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
slides created by Marty Stepp
Chapter 7 – Arrays and Array Lists
Lecture 20: Wrapper Classes and More Loops
Collections.
CompSci 230 S Programming Techniques
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Array List Pepper.
A+ Computer Science AP Review 2018 AP CS A EXAM
CS 106A, Lecture 19 ArrayLists
Multiple Choice -answer the easiest question 1st
Unit-2 Objects and Classes
ArrayLists.
Can store many of the same kind of data together
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
CS Week 9 Jim Williams, PhD.
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.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Welcome to CSE 143!.
CMSC 202 ArrayList Aug 9, 2007.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Arrays.
Java Array Lists 2/13/2017.
Lecture 13: ArrayLists AP Computer Science Principles
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
ArrayLists.
Parsing JSON, Using Libraries, Java Collections, Generics
slides created by Marty Stepp
Building Java Programs
slides created by Alyssa Harding
Review: libraries and packages
Java: Variables, Input and Arrays
Comparing Python and Java
slides created by Marty Stepp
A+ Computer Science AP Review 2019 AP CS A EXAM
Arrays and ArrayLists.
Presentation transcript:

Methods Copying Autoboxing ArrayList Methods Copying Autoboxing

ArrayList ArrayList<Long> bigStuff; bigStuff = new ArrayList<Long>(); List<It> itList; itList = new ArrayList<It>(); In the example above, words can only store String references. decNums can only store Double references. Java knows the exact type of reference in both ArrayLists; thus, there is no need for casting when accessing class specific methods. itList.add(new It(34.21)); out.println(itList.get(0).getIt());

ArrayList OUTPUT h c List<String> ray; ray = new ArrayList<String>(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(ray.get(0).charAt(0)); out.println(ray.get(2).charAt(0)); OUTPUT h c In the example above, ray is an ArrayList that stores String references. Casting would not be required to call non-Object methods on ray. ray.add(0,"hello"); ray.add(1,"chicken"); out.println(ray.get(0).charAt(0)); out.println(ray.get(1).charAt(5)); ray stores String references.

ArrayList Methods

frequently used methods ArrayList frequently used methods Name Use add(item) adds item to the end of the list add(spot,item) adds item at spot – shifts items up-> set(spot,item) put item at spot z[spot]=item get(spot) returns the item at spot return z[spot] size() returns the # of items in the list remove() removes an item from the list clear() removes all items from the list import java.util.ArrayList;

add() one ArrayList<String> words; words = new ArrayList<String>(); words.add("it"); words.add("is"); words.add(0,"a"); words.add(1,"lie"); out.println(words); OUTPUT [a, lie, it, is] The add(item) method adds the new item to the end of the ArrayList. The add(spot, item) method adds the new item at the spot specified. All other existing items are shifted toward the end of the ArrayList. The add method does not override existing values.

add() two List<Integer> nums; nums = new ArrayList<Integer>(); nums.add(34); nums.add(0,99); nums.add(21); nums.add(0,11); out.println(nums); OUTPUT [11, 99, 34, 21] The add(item) method adds the new item to the end of the ArrayList. The add(spot, item) method adds the new item at the spot specified. All other existing items are shifted toward the end of the ArrayList. The add method does not override existing values.

set() ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.set(0,66); ray.add(53); ray.set(1,93); ray.add(22); out.println(ray); OUTPUT [66, 93, 53, 22] The add(item) method adds the new item to the end of the ArrayList. The set(spot, item) method replaces the reference at spot with the new item. The location / address of item is placed in spot. You cannot set a location to a value if the location does not already exist. This will result in an index out of bounds exception.

set() List<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(0, 11); ray.set(5,66); out.println(ray); OUTPUT Runtime exception The add(item) method adds the new item to the end of the ArrayList. The set(spot, item) method replaces the reference at spot with the new item. The location / address of item is placed in spot. You cannot set a location to a value if the location does not already exist. This will result in an index out of bounds exception.

get() OUTPUT23 65 ArrayList<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); out.println(ray.get(0)); out.println(ray.get(3)); OUTPUT23 65 The get(spot) method returns the reference stored at spot. .get(spot) returns the reference stored at spot!

get() OUTPUT23 11 12 65 List<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for(int i=0; i<ray.size(); i++) out.println(ray.get(i)); OUTPUT23 11 12 65 The get(spot) method returns the reference stored at spot. .get(spot) returns the reference stored at spot!

Traditional for loop for (int i=0; i<ray.size(); i++) { out.println(ray.get(i)); } The size() method returns the number of items in the ArrayList. If the ArrayList is storing seven references, size() would return a 7. .size( ) returns the number of elements/items/spots/boxes or whatever you want to call them.

for each loop List<Integer> ray; ray = new ArrayList<Integer>(); ray.add(23); ray.add(11); ray.add(53); for(int num : ray){ out.println(num); } OUTPUT 23 11 53 The new for loop is great to print out Arrays and Collections. The new for loop extracts an item from ray each time it iterates. The new for loop is an iterator based loop. Once the loop reaches the end of ray, it stops iterating.

remove() one ArrayList<String> ray; ray = new ArrayList<String>(); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); out.println(ray); OUTPUT [c, d] The remove method will remove the item at the specified spot / location or the specified value. When an item is removed, all items above the removed item are shifted down toward the front of the ArrayList. All items are shifted to the left. [a, b] becomes [b] [b, c, d] becomes [c, d]

Copying ArrayLists ArrayLists <String>names=new ArrayList<String>(); names.add(“Sponge Bob”); names.add(“Simple Simon”); names.add(“Peter Pan”); ArrayList<String>friends=new ArrayList<String>(names); friends.add(“Cindy Lou”); Now both names and friends reference the same arraylist to which the String “Cindy Lou” was added.

Wrappers and Auto-boxing ArrayList do not accept primitive types! WRAPPER CLASS byte Byte boolean Boolean char Character double Double Float Int Integer long Long short Short ArrayList<Double>nums=new ArrayList<Double>();