Lecter Java: Program design - Chapter 8: Arrays

After completing this unit, you should be able to: Define and initialize one-dimensional and multidimensional arrays, reference and manipulate entire arrays and individual array elements, use arrays and array elements as parameters to methods, present algorithms for sorting and searching arrays,. | Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Background Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional Java provides arrays and the collection classes Consider arrays first Basic terminology List is composed of elements Elements in a list have a common name The list as a whole is referenced through the common name List elements are of the same type — the base type Elements of a list are referenced by subscripting (indexing) the common name Java array features Subscripts are denoted as expressions within brackets: [ ] Base (element) type can be any type Size of array can be specified at run time Index type is integer and the index range must be 0 . n-1 Where n is the number of elements Automatic bounds checking Ensures any reference to an array element is valid Data field length specifies the number of elements in the list Array is an object Has features | Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Background Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional Java provides arrays and the collection classes Consider arrays first Basic terminology List is composed of elements Elements in a list have a common name The list as a whole is referenced through the common name List elements are of the same type — the base type Elements of a list are referenced by subscripting (indexing) the common name Java array features Subscripts are denoted as expressions within brackets: [ ] Base (element) type can be any type Size of array can be specified at run time Index type is integer and the index range must be 0 . n-1 Where n is the number of elements Automatic bounds checking Ensures any reference to an array element is valid Data field length specifies the number of elements in the list Array is an object Has features common to all other objects More robust than arrays in most programming languages Array variable definition styles Without initialization Array variable definition styles With initialization Example Definitions char[] c; int[] value = new int[10]; Causes Array object variable c is un-initialized Array object variable v references a new ten element list of integers Each of the integers is default initialized to 0 c can only reference char arrays v can only reference int arrays Consider int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; (v[2]); v[k] = (); int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.