tailieunhanh - What Is an Array?
một Array là gì? Mảng là một dãy có thứ tự của các nguyên tố. Tất cả các yếu tố trong một mảng có cùng loại (không giống như các trường trong một cấu trúc hoặc lớp, mà có thể có các loại khác nhau). | What Is an Array An array is an unordered sequence of elements. All the elements in an array have the same type unlike the fields in a struct or class which can have different types . The elements of an array live in a contiguous block of memory and are accessed by using an integer index unlike fields in a struct or class which are accessed by name . Declaring Array Variables You declare an array variable by specifying the name of the element type followed by a pair of square brackets followed by the variable name. The square brackets signify that the variable is an array. For example to declare an array of int variables called pins you would write int pins Personal Identification Numbers Microsoft Visual Basic programmers should note that you use square brackets and not parentheses. C and C programmers should note that the size of the array is not part of the declaration. Java programmers should note that you must place the square brackets before the variable name. NOTE You are not restricted to primitive types as array elements. You can also create arrays of structs enums and classes. For example to create an array of Time structs you would use Time times TIP It is useful to give array variable plural names such as places where each element is a Place people where each element is a Person or times where each element is a Time . Creating Array Instances Arrays are reference types regardless of the type of their elements. This means that an array variable refers to an array instance on the heap just as a class variable refers to an object on the heap and does not hold its array elements directly on the stack as a struct does . To review values and references and the differences between the stack and the heap see Chapter 8 Understanding Values and References. Remember that when you declare a class variable memory is not allocated for the object until you create the instance by using new. Arrays follow the same rules when you declare an array variable you do not .
đang nạp các trang xem trước