lecture01-arraylists

Upload: outkast32

Post on 04-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Lecture01-ArrayLists

    1/34

    1

    CSE 2123

    ArrayLists

    Jeremy Morris

  • 8/13/2019 Lecture01-ArrayLists

    2/34

    A programming problem

    Consider the following task:

    You have a file containing a list of purchases

    Maybe from an online ordering system

    You are writing an application that needs to beable to compute:

    The mediandollar amount of the purchases

    The number of purchases where more money was spent

    than median We cant do this as a running total we need to store

    the individual purchases to do the computation

    How can we store a sequence of data values?

    2

  • 8/13/2019 Lecture01-ArrayLists

    3/34

    A programming problem

    We need to use an array

    Recall what we know about arrays

    A sequence of variables of the same data type (e.g. int,

    double, String, boolean, etc.) Each variable in the array is called an element

    Array elements are accessed through their index

    3

    0 9

  • 8/13/2019 Lecture01-ArrayLists

    4/34

    A programming snippet

    Scanner keyboar d = new Scanner ( Syst em. in);

    Syst em. out.print("Enter a filename: ");

    St r i ng f name = keyboar d. next Li ne( ) ;

    Scanner i nFi l e = new Scanner ( new Fi l e( f name) ) ;int numPur chases= i nFi l e. next I nt ( ) ;

    double[] pur chases= new double[numPur chases];

    int count = 0;

    while ( count

  • 8/13/2019 Lecture01-ArrayLists

    5/34

    A programming problem

    Now consider the following task:

    You have a file containing a list of purchases

    Maybe from an online ordering system

    Your application needs to do what it did before Except you dont know the number of elements in the file

    before we read it

    We just need to keep reading elements from the file until

    we reach the end

    How are we going to do that?

    5

  • 8/13/2019 Lecture01-ArrayLists

    6/34

    A programming snippet

    Scanner keyboar d = new Scanner ( Syst em. in);

    Syst em. out.print("Enter a filename: ");

    St r i ng f name = keyboar d. next Li ne( ) ;

    Scanner i nFi l e = new Scanner ( new Fi l e( f name) ) ;int numPur chases= i nFi l e. next I nt ( ) ;

    double[] pur chases= new double[numPur chases];

    int count = 0;

    while ( count

  • 8/13/2019 Lecture01-ArrayLists

    7/34

    A programming problem

    We can use a dynamic array

    Just like an array, except it grows as we need it

    In Java we can use anArrayListto hold this information

    No need to pre-determine the number of elements upfront, just add to the array as we need it

    7

    0

  • 8/13/2019 Lecture01-ArrayLists

    8/34

    A programming problem

    We can use a dynamic array

    Just like an array, except it grows as we need it

    In Java we can use anArrayListto hold this information

    No need to pre-determine the number of elements upfront, just add to the array as we need it

    8

    0 1

  • 8/13/2019 Lecture01-ArrayLists

    9/34

    A programming problem

    We can use a dynamic array

    Just like an array, except it grows as we need it

    In Java we can use anArrayListto hold this information

    No need to pre-determine the number of elements upfront, just add to the array as we need it

    9

    0 2

  • 8/13/2019 Lecture01-ArrayLists

    10/34

    Classes A Light Introduction

    ArrayList is an example of a class

    Youve seen other examples of classes before:

    String

    Scanner File

    Well talk (a lot) more about classes in the coming

    weeks

    The thing to understand now is that a class is acombination of dataand behavior(methods)

    We call this encapsulation(more on that later too)

    10

  • 8/13/2019 Lecture01-ArrayLists

    11/34

    Classes A Light Introduction

    You already know how to use classes:Scanner keyboar d = new Scanner ( Syst em. i n)

    Like any other variable you need to declareit

    Give it a name and a type

    Unlike a primitive type you also need to instantiate

    it

    Thats what that magic word newdoes

    It constructs a new instance of the class

    More about that later too

    For now, just keep the syntax in mind

    11

  • 8/13/2019 Lecture01-ArrayLists

    12/34

    Classes A Light Introduction

    Unlike primitive types, classes are a combination

    of dataand methods

    Primitive types (int, char, boolean, double) only contain

    data

    Methods are all implemented separately

    Each class has a number of associated methods

    Youve already seen this behavior in action:

    St r i ng i nput = keyboar d. next Li ne( ) ;

    next Li ne( ) is a method, one defined by the Scanner

    class

    12

  • 8/13/2019 Lecture01-ArrayLists

    13/34

    ArrayLists

    So what does this have to do with ArrayLists?

    ArrayList is a Java class

    Part of the Java Standard Library

    It implements a dynamic array

    To use an ArrayList we need to know how to use

    it:

    How do we declare it?

    How can we put things into an ArrayList?

    How can we read things from an ArrayList?

    13

  • 8/13/2019 Lecture01-ArrayLists

    14/34

    ArrayList Some Sample codeimport j ava. ut i l . Ar r ayLi st ;

    public static void mai n( St r i ng [ ] ar gs) {

    Ar r ayLi st st r i ngLi st = new Ar r ayLi st ( ) ;

    Scanner keyboar d = new Scanner ( Syst em. i n) ;

    St r i ng i nput = ;

    while ( i nput . equal s( st op) == false) {

    i nput = keyboar d. next Li ne( ) ;

    st r i ngLi st . add( i nput ) ;

    }

    int i = 0;

    whi l e ( i

  • 8/13/2019 Lecture01-ArrayLists

    15/34

  • 8/13/2019 Lecture01-ArrayLists

    16/34

    ArrayList - Declaration

    Lets start with this thing:

    This syntax indicates that the class you are using

    is a genericclass

    Ar r ayLi st - is the placeholder for the class Can be used with any class

    For now, you can think of it as replacing the array type

    declaration:St r i ng[ ] st r i ngAr r = new St r i ng[ 10] ;

    Ar r ayLi st st r i ngL = new Ar r ayLi st ( ) ;

    16

  • 8/13/2019 Lecture01-ArrayLists

    17/34

    ArrayList Some Sample codei mpor t j ava. ut i l . Ar r ayLi st ;

    public static void mai n( St r i ng [ ] ar gs) {

    Ar r ayLi st st r i ngLi st = new Ar r ayLi st ( ) ;

    Scanner keyboar d = new Scanner ( Syst em. i n) ;

    St r i ng i nput = ;

    while ( i nput . equal s( st op) == false) {

    i nput = keyboar d. next Li ne( ) ;

    st r i ngLi st . add( i nput ) ;

    }

    int i = 0;

    whi l e ( i

  • 8/13/2019 Lecture01-ArrayLists

    18/34

    ArrayList - Methods

    Add is the main method used to addelements to an ArrayList:

    bool ean add( E obj )

    Adds an element to the end of the ArrayList

    Returns true if it is able to add it, false if not

    The type E is a generic type must match the type used to

    declare the ArrayList

    No real equivalent in arrays, since arrays arent dynamic

    18

  • 8/13/2019 Lecture01-ArrayLists

    19/34

    ArrayList Some Sample codei mpor t j ava. ut i l . Ar r ayLi st ;

    public static void mai n( St r i ng [ ] ar gs) {

    Ar r ayLi st st r i ngLi st = new Ar r ayLi st ( ) ;

    Scanner keyboar d = new Scanner ( Syst em. i n) ;

    St r i ng i nput = ;

    while ( i nput . equal s( st op) == false) {

    i nput = keyboar d. next Li ne( ) ;

    st r i ngLi st . add( i nput ) ;

    }

    int i = 0;

    whi l e ( i

  • 8/13/2019 Lecture01-ArrayLists

    20/34

    ArrayList - Methods

    The method used to access elements:

    E get ( i nt i ndex)

    Access an element at position index in the ArrayList

    The type E is a generic type must match the type used to

    declare the ArrayList

    Equivalent to accessing an array via a subscript:

    Syst em. out . pr i nt l n( st r i ngAr r [ i ndex] ) ;

    Syst em. out . pr i nt l n( st r i ngL. get ( i ndex) ) ;

    20

  • 8/13/2019 Lecture01-ArrayLists

    21/34

    ArrayList Some Sample codei mpor t j ava. ut i l . Ar r ayLi st ;

    public static void mai n( St r i ng [ ] ar gs) {

    Ar r ayLi st st r i ngLi st = new Ar r ayLi st ( ) ;

    Scanner keyboar d = new Scanner ( Syst em. i n) ;

    St r i ng i nput = ;

    while ( i nput . equal s( st op) == false) {

    i nput = keyboar d. next Li ne( ) ;

    st r i ngLi st . add( i nput ) ;

    }

    int i = 0;

    whi l e ( i

  • 8/13/2019 Lecture01-ArrayLists

    22/34

    ArrayList - Methods

    We also need to have a way to check howlong our ArrayList is:int si ze( )

    Returns back the count of elements in the ArrayList

    Equivalent to using the array length attribute:

    int c = st r i ngAr r . l engt h;

    int c = st r i ngL. si ze( ) ;

    22

  • 8/13/2019 Lecture01-ArrayLists

    23/34

    ArrayList - Methods

    Since ArrayLists are dynamic, theres asecond method for adding elements:

    voi d add( i nt i ndex, E obj )

    Adds an element at position index

    Moves everything currently in the ArrayList from index on to

    the right one position to insert the new element into place

    The type E is a generic type must match the type used todeclare the ArrayList

    No real equivalent in arrays, since arrays arent dynamic

    23

  • 8/13/2019 Lecture01-ArrayLists

    24/34

    ArrayList - Methods

    We also want some way of changingelements at a position:

    E set ( i nt i ndex, E obj )

    Changes the element at position index to the new value

    obj

    The type E is a generic type must match the type used to

    declare the ArrayList

    Returns back what was previously stored at that index Equivalent to using the array subscript:

    st r i ngAr r [ i ndex] = obj ;

    st r i ngL. set ( i ndex, obj ) ;

    24

  • 8/13/2019 Lecture01-ArrayLists

    25/34

    ArrayList - Methods

    Because ArrayLists are dynamic, we candelete elements from them:

    E r emove( i nt i ndex)

    Removes the element at index from the list

    Everything to the right of index is shifted one position left to

    fill the gap

    Returns back what weve removed

    No real equivalent in arrays because arrays are notdynamic

    25

  • 8/13/2019 Lecture01-ArrayLists

    26/34

    ArrayList Example

    Scanner keyboar d = new Scanner ( Syst em. in);

    Syst em. out.print("Enter a filename: ");

    St r i ng f name = keyboar d. nextLi ne( ) ;

    Scanner i nFi l e = new Scanner ( new Fi l e( f name) ) ;

    ArrayList pur chases= new ArrayList();

    while ( i nFi l e. hasNext ( ) ) {

    doubl e pur ch =i nFi l e. next Doubl e( ) ;

    pur chases. add( pur ch) ;

    }

    26

  • 8/13/2019 Lecture01-ArrayLists

    27/34

    Collections - ArrayList

    ArrayLists are used to hold objects

    We cannot use them to hold primitive types

    Ar r ayLi st i nt Li st = new Ar r ayLi st ( ) ;

    27

  • 8/13/2019 Lecture01-ArrayLists

    28/34

    Collections - ArrayList

    ArrayLists are used to hold objects

    We cannot use them to hold primitive types

    Ar r ayLi st i nt Li st = new Ar r ayLi st ( ) ;

    However, each primitive type has a wrapper

    class that we can use

    i nt classI nt eger char classChar act er doubl e classDoubl e

    28

  • 8/13/2019 Lecture01-ArrayLists

    29/34

    Digression Wrapper classes

    Used to transform a primitive typeinto a class

    Useful for generic classes(like collections) that

    require a class and cant use a primitive

    Additionally, wrapper classes have their own

    useful methods

    Example: converting a String to an Integer

    St r i ng r at i ngSt r i ng = 3;I nt eger r at i ngI nt eger = new I nt eger ( r at i ngSt r i ng) ;

    Example: extracting a primitive typei nt pr i mi t i veRat i ng = r at i ngI nt eger . i nt Val ue( ) ;

    29

  • 8/13/2019 Lecture01-ArrayLists

    30/34

    Digression Wrapper Classes

    Similar methods exist for Double:St r i ng r at i ngSt r i ng = 3. 0;

    Doubl e r at i ngDoubl e = new Doubl e( r at i ngSt r i ng) ;

    doubl e pr i mi t i veDoubl e = r at i ngDoubl e. doubl eVal ue( ) ;

    30

  • 8/13/2019 Lecture01-ArrayLists

    31/34

    Digression Wrapper Classes

    Taking a primitive data element and wrappingit up in a wrapper class is known as boxingI nt eger myI nt = new I nt eger ( 6) ;

    Taking the primitive out of the wrapper is

    known as unboxingi nt myPr i mi t i ve = myI nt . i nt Val ue( ) ;

    Java will (usually) automatically do the right

    thing on its own (autoboxing)i nt myPr i mi t i ve = myI nt ;I nt eger myI nt = 6;

    i nt sum = myI nt + 3;

    31

  • 8/13/2019 Lecture01-ArrayLists

    32/34

    ArrayLists

    ArrayLists are used to hold objects

    We cannot use them to hold primitive types

    Ar r ayLi st i nt Li st = new Ar r ayLi st ( ) ;

    Instead we use the wrapper object:

    Ar r ayLi st i nt Li st = new Ar r ayLi st ( ) ;

    But we can use autoboxingto add elements:i nt Li st . add( 12) ;

    i nt val ue = i nt Li st . get ( 0) ;

    32

  • 8/13/2019 Lecture01-ArrayLists

    33/34

    ArrayLists

    If we need an ArrayList of primitives, we use

    the corresponding class instead

    But we can use autoboxingto make our life

    easier adding and examining elements

    public static void mai n( St r i ng [ ] ar gs) {

    Ar r ayLi st i nt Li st =

    new Ar r ayLi st ( ) ;

    i nt Li st . add( 12) ;

    int myI nt = i nt Li st . get ( 0) ;

    }

    33

  • 8/13/2019 Lecture01-ArrayLists

    34/34

    In-class Examplepublic class Ar r ayExampl e {

    public static int count Occur r ences( char [ ] ar r ay, char ch) {

    int chCount =0;

    for ( int l oopCount =0; l oopCount