Lecture 6: ArrayList Implementation
ArrayList<E>. • Standard Java libraries have lots of extra methods not in our implementation. • Many involve working on other collections.
Generic ArrayLists Collections
Oct 3 2016 If the implementation is ... What is the most generic concrete object in Java? ... Implementing ArrayList is almost exactly the same as.
CS 310: ArrayList Implementation
Java has a nice library of containers Collections framework. ? Interfaces that provide get()
Lists -- How to Build Your Own
Your Own. Atul Prakash. Reference: Look at ArrayList.java implementation in Java. Source: http://www.docjar.com/html/api/java/util/ArrayList.java.html
CS 106A Lecture 19 ArrayLists
suggested reading: Java Ch. 11.8 Know how to store data in and retrieve data from an ArrayList. ... ArrayList<String> myArrayList = new ArrayList<>(); ...
ArrayLists
amongst classes eg the Collection interface is implemented by many classes. (LinkedList
CS 211: Generics Using ArrayList
https://cs.gmu.edu/~kauffman/cs211/generics-arraylist.pdf
Hash Table
an ArrayList based dictionary. One way is to use Java's hashCode() method. ... Implement chaining hash table (open hash table) using ArrayList.
CS 310: ArrayList Implementation
Java has a nice library of containers Collections framework. ? Interfaces that provide get()
Big O & ArrayList
specific implementation of a ADT. Fall 2020 For Java folks an ArrayList is like an array
CS 310: ArrayList Implementation
Chris Kauffman
Week 2-2
Logistics
At Home
IRead Weiss Ch 5: Big-O
I Read Weiss Ch 15: ArrayList implementationReminder to DrJava Users IConsider Using GMU Edition of DrJava
I Download here:https://cs.gmu.edu/~kauffman/drjava/Goals IBuild an array list
IAnalyze its complexity
Collections
Java has a nice library of containers,
Collections framew ork
IInterfaces that provideget(),set(),add()
I All have parameterized types:ArrayListAt present, most interested inArrayList
ILike arrays but lacking nice[ ]syntax
IUseget()andset()instead
ICanadd()elements at end (high index)
IDemonstrate ArrayList in DrJava
Basic Premise of the Expandable Array
Use an underlying array
public class MyArrayListT data[];
I datais a standard fixed size array I get()/set()are array opsAdding and Expanding IAdd elements intodata
IIf/whendataruns out of space
1.Allo catea new
la rger a rraydata2 2.Cop yelements f romdatato
data2 3.A ddnew elemen t(s)to data2
4.Set datatodata2
5.Original a rraygets ga rbage
collectedQuestions IWhat"s the notion of
size no w? IHow much should the array grow on expansion?
IIs there wasted space? How much?
CreateMyArrayList
public class MyArrayListIf/whendataruns out of space
1.Allo catea new
la rger a rraydata2 2.Cop yfrom datatodata2
3.A ddnew element(s) to data2
4.Set datatodata2
5.GC gets the old a rrayRespect Mysize()
get()/set()/insert()/remove() must respectsize()which is always smaller than or equal todata.length; check for out of bounds accessExamine Results
ICode up versions together quickly
ISimple version:MyArrayList.javain code distrib
I Also includedjava.util.ArrayListfrom Java 1.7 source IMay also want to look at Weiss"s version in
What are the complexities for methods like
I set(i,x)andget(i) I insert(i,x)andremove(i,x) I add(x) :this is the big oneLimits of Types
Unfortunately, java type system has some limits.new T[10]Not Allowed public class MyArrayListUnsafe Operations in MyArrayList
lila [w01-2-1-code]% javac MyArrayList.java Note: MyArrayList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. lila [w01-2-1-code]% javac -Xlint:unchecked MyArrayList.java MyArrayList.java:77: warning: [unchecked] unchecked cast found : java.lang.Object[] required: T[] this.data = (T[]) new Object[10];1 warning
Unsafe Operations
Suppress Warnings
Offending code is
private T [] data; public MyArrayList(){ this.data=(T[]) new Object[10]; I It is unsafe, but so is fire ITell the compiler to shut up
// I know what I"m doing @SuppressWarnings("unchecked") public MyArrayList(){ this.data=(T[]) new Object[10]; }Alternative:This version uses casting in
get() public class MyArrayListObject [] data;
public MyArrayList(){ this.data =new Object[10]; @SuppressWarnings("unchecked") public T get(int i){ this.rangeCheck(i); return (T) this.data[i];Also needed anywhere else typeT
stuff is returned so less preferred: more@SuppressWarningsHW and Unsafe Operations
I Proper use of generics creates good compile-time type checking IRarely
is casting required; Arra yListimplementation is one such exception I HW1 is NOT such a case IShould not need to caste anything
IShould not need to use@SuppressWarnings
IDoing either may result in penalties
Warmup: Finish methods for MyArrayList
public class MyArrayListThree methods of MyArrayList remain - finish them
I Note common patterns that should be factored into helpers (e.g. expansion, bounds checking) INote:al.insert(i,x)is calledal.add(i,x)in
java.util.ArrayListExercise:ArrayListComplexities
IArrayListof withNelements
ITime/Space Complexities of methods
I Worst-case or Average/AmoritziedWorst Average Worst Average Operation Method Runtime Runtime Space SpaceSize()al.size()Get(i)al.get(i)
Set(i,x)al.set(i,x)
Add(x)al.add(x)
Insert(i,x)al.add(i,x)
Remove(i)al.remove(i)I
What is the space complexity of anArrayListwithN
elements? IIs that a tight bound?
Expanding with Magic Numbers
I Size increase when expansion is required is interesting I Can"t be constant: increase size by 1, or 2, or 10 will not give good complexity IStandard JavaArrayListincreases to3/2*oldSize+1
I Chosen based on engineering experience rather than theory, can use bit shifts to compute it fast IDefaultArrayListsize is 10
IMagic Numbers
: 3/2 and 10, magic because there is no good reason for themAverage/Amortized Complexity
I Worst case complexity forarrayList.add(x)isO(N)when expansion is required I But expansion happ ensra relyif size increase b y150% during expansion I Over many add operations, the averageadd(x)takesO(1) time complexity IAmortized Analysis
: sort of like average case (definition is close enough for this class)quotesdbs_dbs20.pdfusesText_26[PDF] arraylist object java android
[PDF] arraylist object java sort
[PDF] arraylist object javascript
[PDF] arrays in java
[PDF] arrhenius equation activation energy
[PDF] arrhenius equation derivation
[PDF] arrhenius equation example
[PDF] arrhenius equation graph
[PDF] arrhenius equation ln
[PDF] arrhenius equation pdf
[PDF] arrhenius equation r
[PDF] arrhenius equation units
[PDF] arrima
[PDF] arris vip2262 price