Array Basics - PowerPoint PPT Presentation

About This Presentation
Title:

Array Basics

Description:

which are below the average of the 7 temperatures. public static void main(String[] args) ... System.out.println('The average temperature is ' average) ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 19
Provided by: robertw94
Learn more at: https://www.ecs.csun.edu
Category:

less

Transcript and Presenter's Notes

Title: Array Basics


1
Array Basics
2
Arrays
  • An array is a special kind of object used to
    store a collection of data.
  • Arrays are different from other objects in the
    following two ways
  • All the data stored in an array must be of the
    same type.
  • The only methods for an array object are a small
    number of predefined methods which are invoked
    using a special notation.

3
Example Showing How to Compute the Average of
Seven Values
  • int count
  • double next, sum, average
  • Scanner keyboard new Scanner(System.in)
  • System.out.println(Enter 7 values)
  • sum 0
  • for (count 0 count lt 7 count)
  • next keyboard.next.Double()
  • sum sum next
  • average sum/7

4
Comments on Previous Example
  • It works fine if all you want know is the average
    but suppose you want to know how many values are
    above average.
  • In order to do this you must save all the values
    and compare them to the average.
  • You could do this by creating seven variables,
    one for each value, but what if there were 1000
    values? That would be tedious.

5
Creating and Accessing Arrays
  • An array is something like a list of variables,
    but it handles the naming of the variables in a
    nice compact way.
  • In Java, an array is a special kind of object,
    but it is often more useful to think of an array
    as a collection of variables all of the same
    type.
  • For example, an array to serve as a collection of
    seven variables of type double can be created as
    follows
  • double value new double7

6
Creating and Accessing Arrays (contd)
  • This is like declaring the following to be seven
    variables of type double
  • value0, value1, value2, value3,
    value4, value5, value6
  • Note that the number begins with 0 not 1.
  • Each of these variables can be used like any
    other variable of type double, for example
  • value3 32
  • value6 value3 5
  • System.out.println(value6)

7
Creating and Accessing Arrays (contd)
  • However these seven variables are more than just
    seven plain old variables of type double.
  • The number in the square brackets does not have
    to be an integer constant it can be any integer
    expression.
  • That is, you can compute its value.

8
Creating and Accessing Arrays (contd)
  • For example, you could write
  • Scanner keyboard new Scanner(System.in)
  • System.out.println(Enter day number (0-6))
  • int index keyboard.nextInt()
  • System.out.println(Enter temperature for day
    index)
  • temperatureindex keyboard.nextDouble()

9
Creating and Accessing Arrays (contd)
  • These variables with an integer expression in
    square brackets are referred to by many names,
    e.g., indexed variables, elements, or subscripted
    variables.
  • The integer expression in the square brackets is
    called the index or subscript.
  • When we think of these indexed variables grouped
    together into one collective item, we will call
    them an array.

10
Original Example Written Using an Array
  • for (index 0 index lt 7 index)
  • valueindex keyboard.nextDouble()
  • sum sum valueindex

11
Array Details
  • The syntax for creating an array of elements of
    Base_Type is as follows
  • Base_Type Array_Name new Base_TypeLength
  • For example, the following creates an array named
    pressure that is equivalent to 100 variables of
    type int
  • int pressure new int100

12
Array Details (contd)
  • The base type of an array can be of any type.
  • In particular, it can be a class type.
  • For example, we can create an array of type
    Species.
  • Species entry new Species3

13
Example Use of an Array
  • import java.util.public class
    ArrayOfTemperatures / Reads in 7
    temperatures and shows which are above and
    which are below the average of the 7
    temperatures. / public static void
    main(String args) double
    temperature new double7 int index
    double sum, average Scanner
    keyboard new Scanner(System.in)
    System.out.println("Enter 7 temperatures")
    sum 0 for (index 0 index lt 7
    index) temperatureindex
    keyboard.nextDouble( ) sum sum
    temperatureindex average
    sum/7 System.out.println("The average
    temperature is "
    average)
    System.out.println("The temperatures are")
    for (index 0 index lt 7 index)
    if (temperatureindex lt average)
    System.out.println(
    temperatureindex " below average.")
    else if (temperatureindex gt average)
    System.out.println(
    temperatureindex " above average.")
    else //temperatureindex average
    System.out.println(
    temperatureindex " the average.")
    System.out.println("Have a nice week.")

14
The length Instance Variable
  • An array has one public instance variable, called
    length which is equal to the length of the array.
  • For example, for the following array
  • Species entry new Species20
  • entry.length has the value of 20.
  • The use of this variable can help to make you
    program clearer.
  • The length instance variable cannot be changed by
    your program.

15
Array Index Out of Bounds
  • If you try to use a value for an index that is
    outside the range of legal values allowed for
    your array an error will occur.
  • The compiler will not catch the error, but it
    will be discovered when you try to execute the
    statement containing an invalid value for the
    index of an array element.
  • When this happens we say that the index is out of
    bounds.

16
Array Index Out of Bounds (contd)
  • This can happen when you try to fill an array by
    reading in values from the keyboard, for example
  • System.out.println(enter a list of nonnegative
    integers.)
  • System.out.println(Place a negative integer at
    the end.)
  • int a new int10
  • Scanner keyboard new Scanner(System.in)
  • int number keyboard.nextInt()
  • int i 0
  • while (number gt 0)
  • ai number
  • i
  • number keyboard.nextInt()

17
Initializing Arrays
  • An array can be initialized at the time that it
    is declared. For example
  • double reading 3.3, 15.8, 9.7
  • The size of this array is automatically set to 3.
  • The above statement is equivalent to double
    reading new double3
  • reading0 3.3
  • reading1 15.8
  • reading2 9.7

18
Array Index Out of Bounds (contd)
  • A better version of the while loop is as follows
  • while ( (i lt a.length) (number gt 0))
  • ai number
  • i
  • number keyboard.nextInt()
  • if (number gt 0)
  • System.out.println(Could not read in all the
    numbers.)
  • System.out.println(Only read in a.length
    numbers.)
Write a Comment
User Comments (0)
About PowerShow.com