java array programming

Embed Size (px)

Citation preview

  • 8/12/2019 java array programming

    1/31

    1

    Chapter III

    Arrays in Java

    Java ProgrammingCOSC 450

    Dr. Mohamad EL ABED

  • 8/12/2019 java array programming

    2/31

    Chapter III - Objectives

    2

    To use the array data structure to represent lists and tables ofvalues

    To define an array, initialize an array and refer to individual

    elements of an array

    To pass arrays to functions

    To use arrays to store, sort and search lists and tables of

    values

    To define and manipulate multiple-subscripted arrays

  • 8/12/2019 java array programming

    3/31

    Chapter III - Outline

    3

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    4/31

    Arrays: Definition and Declaration

    4

    Definition An arrayis a group of memory locations related by the fact

    that they all have the same nameand the same type

    You can make an array of any type

    int, double, String, etc.

    Declaration

    Type[] table_name = newType[length]

    int [] values = newint [5];

  • 8/12/2019 java array programming

    5/31

    Arrays: Definition and Declaration

    5

    Example

    myList[0] references the first elementin the array

    myList[9] references the last elementin the array

  • 8/12/2019 java array programming

    6/31

    Arrays: Definition and Declaration

    6

    To create an array of a given size, use the operator new:

    int [] values = newint [5];

    Or you may use a variable to specify the size:

    int size = 12;

    int [] values = newint [size];

  • 8/12/2019 java array programming

    7/31

    Chapter III - Outline

    7

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    8/31

    The Length of an Array

    8

    Once an array is created, its size is fixed. It cannot be changed.

    You can find its size using length function

    Example

    int [] values = newint [25];

    values.length returns 25

  • 8/12/2019 java array programming

    9/31

    Chapter III - Outline

    9

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    10/31

    Initializing Arrays with Input Values

    10

    Usingjava.util.Scanner

  • 8/12/2019 java array programming

    11/31

    Chapter III - Outline

    11

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    12/31

    Enhanced for Statement

    12

    The enhanced for statement iterates through the elements of

    an array withoutusing a counter, thus avoiding the possibility

    of stepping outside the array

    Syntaxfor (elementType value: arrayRefVar) {

    // Process the value

    }

    Example

  • 8/12/2019 java array programming

    13/31

    Chapter III - Outline

    13

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    14/31

    Searching Arrays

    14

    Programmers often work with large amounts of data;

    It is necessary to determinewhether an array containsa value

    that matches a certain key value

    The process of finding a particular element of an array is called

    searching

    o Linear search

    o Binary search

  • 8/12/2019 java array programming

    15/31

    Searching an Array with Linear Search

    15

    The linear searchcompares each element of the array with

    the search key

    Since the array is not in any particular order, its just as likely

    that the value will be found in the firstelement as in the last

  • 8/12/2019 java array programming

    16/31

    Searching an Array with Linear Search

    16

    Problem Statement

    Write a function that returns 1 if a given array contains a key

    value passed by the function parameter; 0 otherwise

  • 8/12/2019 java array programming

    17/31

    Searching an Array with Linear Search

    17

    Linear Search Technique disadvantages

    The linear searchingmethod works well for smallor unsorted

    arrays

    However, for largearrays linear searching is inefficient

    If the array is sorted, the high-speed binary searchtechnique

    can be used

  • 8/12/2019 java array programming

    18/31

    Searching an Array with Binary Search

    18

    The binary searchalgorithm eliminatesfrom consideration

    one-halfof the elements in a sortedarray after each comparison

    The algorithm locatesthe middleelement of the array and

    comparesit to thekey value

  • 8/12/2019 java array programming

    19/31

    Searching an Array with Binary Search

    19

    Example 1

    2 1 5 4 7Array:

    Key value: 7

    Sorted Array:

    11 3

    1 2 3 4 5 7 11

    The array contains 7

  • 8/12/2019 java array programming

    20/31

    Searching an Array with Binary Search

    20

    Example 2

    2 1 5 4 7Array:

    Key value: 6

    Sorted Array:

    11 3

    1 2 3 4 5 7 11

    The array do not contains 6

  • 8/12/2019 java array programming

    21/31

    Linear Search vs. Binary Search

    21

    In a worstcase-scenario, searchingan array of 1024 elements

    takes:

    o 1024 comparisons using a linear search

    o 10 comparisons using a binary search

  • 8/12/2019 java array programming

    22/31

    Chapter III - Outline

    22

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    23/31

    Multidimensional Arrays

    23

    Arrays in C++ can have multiplesubscripts

    A common use of multidimensional arrays is to represent

    tablesof values consisting of information arranged in rows

    and columns

    To identify a particular table element, we must specify two

    subscripts: The firstidentifies the elements rowand the

    secondidentifies the elements column

  • 8/12/2019 java array programming

    24/31

    Multidimensional Arrays

    24

    In general, an array with m rows and n columns is called anm-by-n array

    An example of double-subscripted array

    int[][] a = newint[3][4];

  • 8/12/2019 java array programming

    25/31

    Multidimensional Arrays

    25

    Or you may use variables to specify the 2D array:

  • 8/12/2019 java array programming

    26/31

    Multidimensional Arrays

    26

    Uneven rows

    One consequence of 2D arrays is that each row can be a

    different size

    Example Lower Triangular array

  • 8/12/2019 java array programming

    27/31

    Chapter III - Outline

    27

    Arrays: Definition and Declaration

    The Length of an Array

    Initializing Arrays with Input Values

    Enhanced for Statement

    Searching Arrays

    Multidimensional Arrays

    Variable-Length Argument Lists

  • 8/12/2019 java array programming

    28/31

    Variable-Length Argument Lists

    28

    With variable-length argument lists, you can create methods

    that receive an unspecified number of arguments

    A type followed by an ellipsis () in a methods parameter list

    indicates that the method receives a variablenumber of

    arguments of that particular type

  • 8/12/2019 java array programming

    29/31

    Variable-Length Argument Lists

    29

    Example

  • 8/12/2019 java array programming

    30/31

    Variable-Length Argument Lists

    30

    Common Programming Error

    Placing an ellipsisin the middle of a parameter list is a syntax

    error

    An ellipsismay be placed only at the end of the parameter

    list

  • 8/12/2019 java array programming

    31/31

    31

    End of Chapter III