TAILIEUCHUNG - Lecture An introduction to computer science using java (2nd Edition): Chapter 8 - S.N. Kamin, D. Mickunas, E. Reingold

Chapter 8 - One-dimensional arrays. In this chapter we will: introduce the array as a structure for storing large amounts of data, discuss common array operations, introduce algorithms for searching and sorting arrays, show how multiple images can be painted from an array to use in programming simple animations. | Chapter 8 One-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by . Kamin, D. Mickunas, E. Reingold Chapter Preview In this chapter we will: introduce the array as a structure for storing large amounts of data discuss common array operations introduce algorithms for searching and sorting arrays show how multiple images can be painted from an array to use in programming simple animations Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples: int[] counts; double[] scores; String[] studentNames; Array Allocation Arrays are allocated using the Java new operator The syntax is: new type[size]; Examples: counts = new int[10]; scores = new double[15]; studentNames = new String[10]; Array Organization 0 1 2 3 4 5 6 7 8 9 counts Each box is an int variable The numbers on top are each variable’s subscript or index An array of size 10 has subscripts 0 to 9 Array Subscripts Arrays can contain any one type of value (either primitive values or references) Subscripts are used to access specific array values Examples: counts[0] // first variable in counts counts[1] // second variable in counts counts[9] // last variable in counts counts[10] // error – trying to access // variable outside counts Expressions as Subscripts Array subscripts do not have to be constants Array subscripts do need to be integer expressions that evaluate to valid subscript values for the current array allocation Examples: counts[i] counts[2*i] counts[I/2] Array Initialization Arrays can be initialized by giving a list of their elements If your list contains n elements the subscripts will range from 0 to n – 1 You do not need to allocate the array explicitly after it is initialized Example: int [] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; Initializing an Array of Strings final Sring[ ] NAME = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.