1 / 11

Introduction to Arrays

Chapter 10. Introduction to Arrays. What is an array?. An array is an ordered collection that stores many elements of the same type within one variable name. Elements are the items in an array. Each element is identified with an index number.

Télécharger la présentation

Introduction to Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Introduction to Arrays

  2. What is an array? • An array is an ordered collection that stores many elements of the same type within one variable name. • Elements are the items in an array. • Each element is identified with an index number. • Index numbers always start at 0 for the first element.

  3. Declaring Arrays • Identify type of array • Use square brackets • int[] myArray; • Declares an array of int values

  4. Instantiating Arrays • Use the new keyword • Identify number of elements • myArray = new int[1000]; • Instantiates myArray with 1000 elements • You can declare and instantiate in one line • int[] myArray = new int[1000];

  5. Putting Values in the Array • Use the index operator to assign a value at a particular place in the array. • myArray[10] = 100; • Assigns the value 100 to the 10th element in the array • Most programs use a for loop to iterate the array.

  6. Initializing the Array • You can initialize the array with values at the same time the array is created. • int[] myArray = {1, 2, 3, 4, 5};

  7. Determining the Length of the Array • Use the length property to find the number of elements in the array. • System.out.println(myArray.length); • Displays the length of the array in the console • For ( int i = 0; i < myArray.length; i++ ); • Uses the length property to set up the for loop

  8. Passing by Value • Intrinsic types are passed to methods by value. • A copy of the value is passed. • The original value is unchanged.

  9. Passing by Reference • Objects are passed to methods by reference. • The reference points to the original object. • A change to the object affects the object in the calling method.

  10. Passing Arrays to Methods • An array is an object type. • Passed by reference • Elements of arrays are not necessarily passed by reference. • Depends on the underlying element type • Extracting int elements from an array and passing them to a method passes them by value

  11. Using Polymorphic Objectsin an Array • An array can hold objects that: • Derive from the same base class • Implement a common interface

More Related