Xem mẫu

Chapter 10 – Arrays and ArrayLists 1 Array Basics Array Declaration Array Creation Array Element Initialization Array Default Values Array length Property Partially Filled Arrays Copying an Array Searching an Array Sorting an Array Selection Sort Chapter 10 – Arrays and ArrayLists 2 Two­Dimensional Arrays Arrays of Objects The ArrayList Class How to Create an ArrayList Object Adding Elements to an ArrayList Object How to Access an Element Within an ArrayList How to Update an ArrayList Object Additional ArrayList Methods Printing or Concatenating an ArrayList Storing Primitives in an ArrayList ArrayList Example Using Anonymous Objects and the For­Each Loop ArrayList Objects Versus Standard Arrays Array Basics 3 A class stores a group of related data, and it stores the methods that operate on that data. An array is a limited version of a class. Like a class, an array also stores a group of related data, but an array does not store methods. Another difference between an array and a class is that an array`s data must all be of the same type. Here`s a picture of an array that holds a list of phone numbers. Each of the five boxes is called an array element and each box stores one phone number. phoneList 8167412000 first phone number 2024561111 7852963232 8008675309 0035318842133 last phone number Array Basics 4 A class uses dot notation to access one of its members. On the other hand, an array uses square brackets around an index to access one of its elements. The rightmost column shows how to access each of the 5 elements in the phoneList array. Note that the index values start at 0 instead of 1 and the last index value is one less than the number of elements in the array. index phoneList 0 8167412000 1 2024561111 2 7852963232 3 8008675309 4 0035318842133 how to access each element phoneList[0] phoneList[1] phoneList[2] 5 elements phoneList[3] phoneList[4] Array Basics 5 Here`s how you can change the first phone number to 2013434: phoneList[0] = 2013434; And here`s how you can print the second phone number: System.out.println(phoneList[1]); ... - tailieumienphi.vn
nguon tai.lieu . vn