+ All Categories
Home > Documents > Introduction to Data Structures 2

Introduction to Data Structures 2

Date post: 04-Jun-2018
Category:
Upload: aamir2686
View: 221 times
Download: 0 times
Share this document with a friend

of 17

Transcript
  • 8/13/2019 Introduction to Data Structures 2

    1/17

    Data structures and Algor

  • 8/13/2019 Introduction to Data Structures 2

    2/17

    Data structures and Algorithms

    Computer science emphasizes two important topics: datastructalgorithms. Those topics are important because the choices you

    program's datastructures and algorithms affect that program's usage (for datastructures) and CPU time (for algorithms that intthose datastructures).

  • 8/13/2019 Introduction to Data Structures 2

    3/17

    Data structures and Algorithms

    When choosing a datastructure or algorithm, you sometimes dinverse relationship between memory usage and CPU time: thememory a datastructure uses, the more CPU time associated alneed to process the datastructure's data items.Also, the more mdatastructure uses, the less CPU time associated algorithms nethe data itemsand faster algorithms result.

  • 8/13/2019 Introduction to Data Structures 2

    4/17

    Data structures and Algorithms

  • 8/13/2019 Introduction to Data Structures 2

    5/17

    The Array data structure

    The array is one of the most widely used data structures becausflexibility in deriving complex data structures and its simplicity.

    an arrayis a sequence of elements, where each element(a groubytes that stores a single data item) associates with at least one(nonnegative integer).

  • 8/13/2019 Introduction to Data Structures 2

    6/17

    The Array data structure

    That definition raises four interesting points:

    Each element occupies the same number of bytes; the exact number the type of the element's data item.

    All elements share the same type.

    We tend to think of an array's elements as occupying consecutive melocations. When I discuss two-dimensional arrays, you'll discover thatthe case.

    The number of indexes associated with any element is the array's dim

  • 8/13/2019 Introduction to Data Structures 2

    7/17

    One-dimensional arrays

    The simplest kind of array has one dimension: each element associates

    index. Java provides three techniques for creating a one-dimensional ar

    use only an initializer.

    use only keyword new.

    use keyword new with an initializer.

    To create a one-dimensional array with only an initializer can be done u

    Object-Oriented Language, We focus on the latter two techniques in th

  • 8/13/2019 Introduction to Data Structures 2

    8/17

    One-dimensional arrays (use only keywor

    Use only keyword new to create a one-dimensional array.That requires either of the following syntaxes:

    type variable_name [ ] = new type [ integer_expression ] ;

    type [ ] variable_name = new type [ integer_expression ] ;

  • 8/13/2019 Introduction to Data Structures 2

    9/17

    One-dimensional arrays (use only keywor

    Specifies variable_name as the name of the one-dimensional array va

    Specifies type as each element's type. Because the one-dimensional holds a reference to the one-dimensional array, the variable's type is

    Specifies keyword new, followed by type, followed by integer_expres

    square brackets ([ ]), which identifies the number of elements. new almemory for a one-dimensional array's elements and zeroes all bits inelement's bytes, which means that each element contains a default vinterpret based on type.

    Specifies = to assign the one-dimensional array's reference to variabl

  • 8/13/2019 Introduction to Data Structures 2

    10/17

    One-dimensional arrays (use only keywor

    The following code fragment use only keyword new to create a on

    dimensional array that stores data items based on a primitive type int [] test_scores = new int [4];

  • 8/13/2019 Introduction to Data Structures 2

    11/17

    One-dimensional arrays (use only keywor

    arrays store data items that reference objects. The following codeuses only keyword new to create a pair of one-dimensional arrays store data items based on a reference type:

    Clock [] c1 = new Clock [3];

    Clock [] c2 = new AlarmClock [3];

  • 8/13/2019 Introduction to Data Structures 2

    12/17

    One-dimensional arrays (use keyword new with a

    Use keyword new with an array initializer to create a one-dime

    array.That technique requires either of the following syntaxes:

    type variable_name [ ] = new type [ ] initializer ;

    type [ ] variable_name = new type [ ] initializer ;

    where initializer has the following syntax:

    { [ initial_value [ , ... ] ] }

  • 8/13/2019 Introduction to Data Structures 2

    13/17

    One-dimensional arrays (use keyword new with a

    Specifies variable_name as the name of the one-dimensional array variable.

    Specifies type as the type of each element. Because the one-dimensional arholds a reference to the one-dimensional array, the variable's type is type [ ]

    Specifies keyword new, followed by type, followed by empty square bracket

    initializer. You do not specify the number of elements between the square brthe compiler determines that count from the number of initializer's entries. memory for a one-dimensional array and assigns each of initializer's entries in a left-to-right fashion.

    Specifies = to assign the one-dimensional array's reference to variable_nam

  • 8/13/2019 Introduction to Data Structures 2

    14/17

    One-dimensional arrays (use keyword new with a

    The following code fragment uses keyword new with an array initcreate a one-dimensional array that stores data items based on a

    type:

    int [] test_scores = new int [] { 70, 80, 20, 30 };

  • 8/13/2019 Introduction to Data Structures 2

    15/17

    One-dimensional arrays (use keyword new with a

    String [] months = new String [] { "Jan", "Feb", "Mar", "Apr", "Ma"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    System.out.println (months [0]); // Output: Jan

  • 8/13/2019 Introduction to Data Structures 2

    16/17

    One-dimensional arrays (use keyword new with a

    // The following method call results in an ArrayIndexOutOfBoundsExc

    // because index equals the month's length

    System.out.println (months [months.length]);

    System.out.println (months [months.length - 1]); // Output: Dec // The following method call results in an ArrayIndexOutOfBoundsExc

    // because index < 0

    System.out.println (months [-1]);

  • 8/13/2019 Introduction to Data Structures 2

    17/17


Recommended