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

Chapter 5 Arrays

28 452 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

Arrays Arrays Chapter 5 Outline  Declaring and using array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Array • An array is a data structure that stores a collection of values of the same type • Arrays are accessed with [ ]  Array indices are zero-based  Example: scores[5] returns the 5 th value in the array 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 scores The entire array has a single name Each value has an index Declaring an array • Syntax: • Example:  int[] scores = new int[100]; • an array that can hold 100 integers  String[] names; names = new names[20]; • an array that can hold 20 String objects • You can create an array object and supply initial values at the same time  int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };  char[] letterGrades = {'A', 'B', 'C', 'D', ’F'}; DataType[] arrayName = new DataType[size]; DataType[] arrayName; // only declares the variable arrayName = new DataType[size]; See ebook page 328 for other syntax Using the array • If a is an array, to process all elements in a, using: • Example:  for (int i=0; i < scores.length; i++) System.out.println (scores[i]);  for (int score : scores) System.out.println (score);  for (String n : names) System.out.println (n); • See BasicArray.java, page 322. Modify the program to input values for (int i=0; i < a.length; i++) // process a[i] for (Type x : a) // process x length field returns the number of elements Bounds checking • Once an array is created, it has a fixed size • The index value must be in range 0 to N-1, so you cannot access indexes outside this limit  The Java interpreter throws an exception named ArrayIndexOutOfBoundsException if an array index is out of bounds • This is called automatic bounds checking • It’s common error when using arrays: for (int i=0; i <= codes.length; i++) codes[i] = i*50 + epsilon; problem Array as Parameters • An entire array can be passed as a parameter to a method • Because an array is an object, when an entire array is passed as a parameter, a copy of the reference to the original array is passed (they become aliases of each other)  Therefore, changing an array element within the method changes the original • Example: write inputArray and ouputArray methods, see Mang1Chieu.java Outline  Declaring and using array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Array of objects • The elements of an array can be object references  String[] words = new String[10]; • words object stores 10 references to String objects  Rectangle[] rects = new Rectangle[5]; • rects object stores 5 references to Rectangle objects • Initially an array of objects holds null references • Each object stored in an array must be instantiated separately Array of String • The words array when initially declared: words - - - - - • At this point, the following reference would throw a NullPointerException: System.out.println (words[0]); String[] words = new String[10]; [...]... rectangle Rectangle[] rects = new Rectangle [5] ; rects[0] = new HinhChuNhat (5, 4); rects[1] = new HinhChuNhat(7,3); rects[2] = new HinhChuNhat(100 ,50 ); (5, 4) rects (7,3) (100 ,50 ) - Example: Array of objects • Now let's look at an example that manages a collection of CD objects  See CD.java (page 340)  See CDCollection.java (page 337)  See Tunes.java (page 3 35) • Modify main method to add a CD object... (page 341)  insertion sort (page 3 45) • To sort an array of numbers, you can use one of the sort methods in the Arrays class (using a tuned QuickSort algorithm)  Arrays. sort(type[] a): a is an array of type int, long, short, char, byte, float or double  Example: • int[] a = new int[10000]; … Arrays. sort(a); • How to sort an array of objects by using sort method in the Arrays class? Sorting an array of... other.getArea()) return -1; else if (getArea() > other.getArea()) return 1; return 1; } } Searching value in the array • To search a value in a sorted array, you can use binarySearch method in the Arrays class  int Arrays. binarySearch( type[] a, type v ) • a: a sorted array of type int, long, short, char, byte, float or double • v: a value of the same type as the elements of a • If it is found, its index... using array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Two-Dimensional Arrays • A two-dimensional array can be thought of as a table of elements, with rows and columns two int[][] scores = new int[12] [50 ]; dimensions • A two-dimensional array is declared by specifying the size of each dimension separately: • A array element is referenced... a = new int[10000]; … Arrays. sort(a); • How to sort an array of objects by using sort method in the Arrays class? Sorting an array of objects • To sort an array of objects by using sort method in the Arrays class, the objects must belong to classes that implement the Comparable interface  Example: you want to sort an array of Rectangle order by area ascending public class Rectangle implements Comparable... dimension separately: • A array element is referenced using two index values: value = scores[3][6]; Example public class TwoDArray { public static void main (String[] args) { int[][] table = new int [5] [10]; for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length;... array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Why do we use ArrayList class? • Problem:  In C/C++, you have to fix the sizes of all arrays at compile time • forces users into uncomfortable  In Java, the situation is much better You can set the size of an array at run time int size= ; Rectangle[] rects = new Rectangle[size]; • once... array, but it automatically adjusts its capacity as you add and remove elements, without your needing to write any code Declaring an ArrayList object • Syntax: ArrayList name = new ArrayList(); • As of JDK 5. 0, ArrayList is a generic class with a type parameter, so you must to specify the type of the element objects that the array list holds ArrayList name = new ArrayList();  ArrayList . Arrays Arrays Chapter 5 Outline  Declaring and using array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Array • An array. returned Outline  Declaring and using array  Array of objects  Sorting and seaching elements in an array  Two-dimensional array  The ArrayList class Two-Dimensional Arrays • A two-dimensional array can be thought. int[10000]; … Arrays.sort(a); • How to sort an array of objects by using sort method in the Arrays class? Sorting an array of objects • To sort an array of objects by using sort method in the Arrays

Ngày đăng: 13/05/2014, 10:42

Xem thêm: Chapter 5 Arrays

TỪ KHÓA LIÊN QUAN

Mục lục

    Example: Array of objects

    Sorting elements in an array

    Sorting an array of objects

    Searching value in the array

    Why do we use ArrayList class?

    Declaring an ArrayList object

    Methods of the ArrayList

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

TÀI LIỆU LIÊN QUAN