1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter Introduction Arrays

34 170 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Chapter : Arrays Slide 2 9:06:37 AM 1. Introduction Arrays • A group of contiguous memory locations – Same name – Same type • Refer to particular element in the array by position number • Refer to any element by giving the name of the array followed by the position number ( subscript ) of the element in square brackets ([ ]) • First element is the zeroth element – First element of array c is c[ 0 ] Slide 3 9:06:37 AM Arrays Fig. 7.1 A 12-element array. -45 6 0 72 1543 -89 0 62 -3 1 6453 -78 Position number (index or subscript) of the element within array c Name of array (Note that all elements of this array have the same name, c) c[ 11 ] c[ 10 ] c[ 9 ] c[ 8] c[ 7 ] c[ 4 ] c[ 3 ] c[ 2 ] c[ 1 ] c[ 0 ] c[ 6 ] c[ 5 ] Slide 4 9:06:37 AM Declaring and Creating Arrays • Programmer specifies the type of the elements of the array • new operator to allocate dynamically the number of elements in the array • Array declarations and initializations need not be in the same statement • In arrays of value types, each element contains one value of the declared type Slide 5 9:06:37 AM Creating an Array • Declare the array variable <Datatype> [ ] <NameArray>; Ex: int[ ] c; • Create the array; assign to array variable <NameArray> = new <Datatype> <[ARRAY_SIZE]>; Ex : c = new int[ 12 ]; int [ ] c = new int[ 12 ]; Slide 6 9:06:37 AM Examples Using Arrays Slide 7 9:06:37 AM Examples Using Arrays Slide 8 9:06:37 AM Properties and methods of array • array_name.Length: return size of array • array_name.GetValue (int index): return an object at position index. • array_name[int index]: return an object at position index. • array_name.SetValue (object value, int index): add or modify an object at position index • array_name[int index] = value: add or modify an object at position index Slide 9 9:06:37 AM Passing Arrays to Methods • Pass arrays as arguments to methods by specifying the name of the array (no brackets) • Arrays are passed by reference • Individual array elements are passed by value Slide 10 9:06:37 AM Passing Arrays to Methods [...]...Passing Arrays to Methods 2 Multiple-Subscripted Arrays • Require two or more subscripts to identify a particular element • Arrays that req2uire two subscripts to identify an element are called doublesubscripted arrays • Rectangular arrays – Often represent tables in which each row is the same size and each column is the... Multiple-Subscripted Arrays • GetLength (int x) – GetLength(0) – GetLength(1) Multiple-Subscripted Arrays (Jagged Arrays) DataType[ ][ ] ArrayName; ArrayName = new DataType[Size][ ]; DataType[ ][ ] ArrayName = new DataType[Size][ ]; Example Example Example foreach Repetition Structure •The foreach repetition structure is used to iterate through values in data structures such as arrays •No counter •A... element’s row and the second subscript the element’s column Multiple-Subscripted Arrays Column 0 Column 1 Column 2 Column 3 Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3] Row 1 a[1, 0] a[1, 1] a[1, 2] a[1, 3] Row 2 a[2, 0] a [2, 1] a[2, 2] a[2, 3] Column index (or subscript) Row index (or subscript) Array name Multiple-Subscripted Arrays DataType[ , ] ArrayName; ArrayName = new DataType [rowSize, colSize]; DataType . ] c[ 9 ] c[ 8] c[ 7 ] c[ 4 ] c[ 3 ] c[ 2 ] c[ 1 ] c[ 0 ] c[ 6 ] c[ 5 ] Slide 4 9:06:37 AM Declaring and Creating Arrays • Programmer specifies the type of the elements of the array • new operator. tables in which each row is the same size and each column is the same size – By convention, first subscript identifies the element’s row and the second subscript the element’s column Slide. - 45 6 0 72 154 3 -89 0 62 -3 1 6 453 -78 Position number (index or subscript) of the element within array c Name of array (Note that all elements of this array have the same name, c) c[ 11 ] c[ 10 ] c[

Ngày đăng: 13/05/2014, 11:39

Xem thêm: Chapter Introduction Arrays

Mục lục

    Declaring and Creating Arrays

    Properties and methods of array

    Passing Arrays to Methods

    Multiple-Subscripted Arrays (Jagged Arrays)

    ArrayList (Danh sách mảng)

    Các thao tác với ArrayList

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN