Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.

Slides:



Advertisements
Similar presentations
Chapter 11 Arrays. Introduction Array: An ordered collection of values with two distinguishing characters: – Ordered and fixed length – Homogeneous. Every.
Advertisements

The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R slides partially adapted from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Building Java Programs
Arrays And ArrayLists - S. Kelly-Bootle
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
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.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
CSE 1201 Object Oriented Programming ArrayList 1.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
EKT472: Object Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
(like an array on steroids)
Arrays and ArrayLists Eric Roberts CS 106A February 12, 2010.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Building Java Programs
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Programming in Java Lecture 11: ArrayList
Building Java Programs
Building Java Programs
CMSC 202 ArrayList Aug 9, 2007.
ArrayList Collections.
Lecture 2: Implementing ArrayIntList reading:
CMSC 202 ArrayList Aug 9, 2007.
slides created by Ethan Apter
ArrayLists 22-Feb-19.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
ArrayLists 27-Apr-19.
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CS 200 Objects and ArrayList
Presentation transcript:

Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight of the product –Price of the product –Currency of the product price The name and weight should be assigned as part of the constructor call, and it should not be possible to change them subsequently. By default, price of each product is 10 TL. The class should have appropriately getters for all four fields and setters for the last two.

Exercise: Design a Stock Class Create a definition for a class called Stock using Arrays, which keeps track of the information about products exist in a store: –Name of the Store –The products exist in the store The name of the store and initial number of products should be assigned as part of the constructor call. Implement the following methods in the Stock class: –Get Store Name –Get number of products –Add a new product

Chapter 11—Arrays and ArrayLists The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R 1 1 Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There ’ s a green one and a pink one and a blue one and a yellow one And they're all made out of ticky-tacky and they all look just the same —Malvina Reynolds, “ Little Boxes, ” 1962 Özyeğin University Slides are adapted from the originals available at -> ozucs102 3

The ArrayList Class Although arrays are conceptually important as a data structure, they are not used as much in Java as they are in most other languages. The reason is that the java.util package includes a class called ArrayList that provides the standard array behavior along with other useful operations. The main differences between Java arrays and ArrayList s stem from the fact that ArrayList is a Java class rather than a special form in the language. As a result, all operations on ArrayList s are indicated using method calls. For example, the most obvious differences include: –You create a new ArrayList by calling the ArrayList constructor. –You get the number of elements by calling the size method rather than by selecting a length field. –You use the get and set methods to select individual elements. The next slides summarize the most important methods in the ArrayList class. The notation indicates the base type > ozucs102

Methods in the ArrayList Class boolean add( element) Adds a new element to the end of the ArrayList ; the return value is always true. void add(int index, element) Inserts a new element into the ArrayList before the position specified by index. remove(int index) Removes the element at the specified position and returns that value. boolean remove( element) Removes the first instance of element, if it appears; returns true if a match is found. void clear() Removes all elements from the ArrayList. int size() Returns the number of elements in the ArrayList. get(int index) Returns the object at the specified index. set(int index, value) Sets the element at the specified index to the new value and returns the old value. int indexOf( value) Returns the index of the first occurrence of the specified value, or - 1 if it does not appear. boolean contains( value) Returns true if the ArrayList contains the specified value. boolean isEmpty() Returns true if the ArrayList contains no elements > ozucs102

The ArrayList Class int[] array1 = new int[5]; ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { array1[i] = i; } for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } 6 -> ozucs102

The ArrayList Class int[] array1 = new int[5]; for(int i = 0; i < 5; i++) { array1[i] = i; } > ozucs102

The ArrayList Class int[] array1 = new int[5]; for(int i = 0; i < 5; i++) { array1[i] = i; } > ozucs102

The ArrayList Class int[] array1 = new int[5]; for(int i = 0; i < 5; i++) { array1[i] = i; } > ozucs102

The ArrayList Class int[] array1 = new int[5]; for(int i = 0; i < 5; i++) { array1[i] = i; } > ozucs102

The ArrayList Class int[] array1 = new int[5]; for(int i = 0; i < 5; i++) { array1[i] = i; } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102 int vs. Integer

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); } > ozucs102

The ArrayList Class int[] array1 = new int[5]; ArrayList arrayList1 = new ArrayList (); for(int i = 0; i < 5; i++) { array1[i] = i; println(array1.length); } println(“ ”) for(int i = 0; i < 5; i++) { // add a new element to the end of the list arrayList1.add(i); println(arrayList1.size()); } add method extends the size by one by adding a new element to the end of the list > ozucs102

The ArrayList Class array[2] = 42; println(array[2]); arrayList.set(2, 42); println(arrayList.get(2)); > ozucs102

The ArrayList Class ArrayList arrayList = new ArrayList (); arrayList.add(42); arrayList.add(43); arrayList.add(44); arrayList.set(2, 45); println(arrayList.get(2)); // prints 45 ArrayList arrayList = new ArrayList (); arrayList.add(42); arrayList.add(43); println(arrayList.get(2)); // ERROR!!! arrayList.add(44); ArrayList arrayList = new ArrayList (); arrayList.add(42); arrayList.add(43); arrayList.set(2, 45); // ERROR!!! arrayList.add(44); println(arrayList.get(2)); > ozucs102 ? ? ?

The ArrayList Class ArrayList intList = new ArrayList (); ArrayList doubleList = new ArrayList (); ArrayList stringList = new ArrayList (); intList.add(42); doubleList.add(42.0); stringList.add(“42”); > ozucs102

import java.util.*; /** * This program reads in a list of integers and then displays that list in * reverse order. This version uses an ArrayList to hold the values. */ public class ReverseArrayList { public static void main(String[] args) { println("This program reverses the elements in an ArrayList."); println("Use " + SENTINEL + " to signal the end of the list."); ArrayList list = readArrayList(); reverseArrayList(list); printArrayList(list); } /* Reads the data into the list */ public static ArrayList readArrayList() { ArrayList list = new ArrayList (); while (true) { int value = readInt(" ? "); if (value == SENTINEL) break; list.add(value); } return list; } skip code page 1 of 2 Reversing an ArrayList (Java 5.0) > ozucs102

import acm.program.*; import java.util.*; /** * This program reads in a list of integers and then displays that list in * reverse order. This version uses an ArrayList to hold the values. */ public class ReverseArrayList extends ConsoleProgram { public void run() { println("This program reverses the elements in an ArrayList."); println("Use " + SENTINEL + " to signal the end of the list."); ArrayList list = readArrayList(); reverseArrayList(list); printArrayList(list); } /* Reads the data into the list */ private ArrayList readArrayList() { ArrayList list = new ArrayList (); while (true) { int value = readInt(" ? "); if (value == SENTINEL) break; list.add(value); } return list; } /* Prints the data from the list, one element per line */ public static void printArrayList(ArrayList list) { for (int i = 0; i < list.size(); i++) { int value = list.get(i); println(value); } /* Reverses the data in an ArrayList */ public static void reverseArrayList(ArrayList list) { for (int i = 0; i < list.size() / 2; i++) { swapElements(list, i, list.size() - i - 1); } /* Exchanges two elements in an ArrayList */ public static void swapElements(ArrayList list, int p1, int p2) { int temp = list.get(p1); list.set(p1, list.get(p2)); list.set(p2, temp); } /* Private constants */ private static final int SENTINEL = 0; } Reversing an ArrayList (Java 5.0) page 2 of > ozucs102

Methods in the ArrayList Class boolean add( element) Adds a new element to the end of the ArrayList ; the return value is always true. void add(int index, element) Inserts a new element into the ArrayList before the position specified by index. remove(int index) Removes the element at the specified position and returns that value. boolean remove( element) Removes the first instance of element, if it appears; returns true if a match is found. void clear() Removes all elements from the ArrayList. int size() Returns the number of elements in the ArrayList. get(int index) Returns the object at the specified index. set(int index, value) Sets the element at the specified index to the new value and returns the old value. int indexOf( value) Returns the index of the first occurrence of the specified value, or - 1 if it does not appear. boolean contains( value) Returns true if the ArrayList contains the specified value. boolean isEmpty() Returns true if the ArrayList contains no elements > ozucs102

Generic Types in Java 5.0 The notation used on the preceding slide is a new feature of Java that was introduced with version 5.0 of the language. In the method descriptions, the notation is a placeholder for the element type used in the array. Class definitions that include a type parameter are called generic types. The advantage of specifying the element type is that Java now knows what type of value the ArrayList contains. When you call set, Java can ensure that the value matches the element type. When you call get, Java knows what type of value to expect, eliminating the need for a type cast. When you declare or create an ArrayList, it is a good idea to specify the element type in angle brackets. For example, to declare and initialize an ArrayList called names that contains elements of type String, you would write ArrayList names = new ArrayList (); > ozucs102

import java.util.ArrayList; public class Experiments { public static void main(String[] args) { ArrayList list = new ArrayList (); list.add(42); list.add(43); printArrayList(list); list.add(0, 44); printArrayList(list); list.add(1, 45); printArrayList(list); list.remove(1); printArrayList(list); list.remove(new Integer(42)); printArrayList(list); //list.remove(43); // ERROR!!! //printArrayList(list); ArrayList list2 = new ArrayList (); list2.add("a"); list2.add("b"); list2.add("c"); list2.add("d"); printStringArrayList(list2); list2.remove(1); printStringArrayList(list2); list2.remove("c"); printStringArrayList(list2); } > ozucs102

public static void printStringArrayList(ArrayList list) { for(int i = 0; i < list.size(); i++){ System.out.print(" " + list.get(i)); } System.out.println(); } public static void printArrayList(ArrayList list) { for(int i = 0; i < list.size(); i++){ System.out.print(" " + list.get(i)); } System.out.println(); } > ozucs102