1 / 23

Arrays

Arrays. Arrays. Most real-world programs handle vast amounts of data. Fortunately, data usually can be organized and processed systematically. Arrays are almost always used for this. Arrays Topics. The idea of arrays. Array declaration. Array declaration and construction.

fancy
Télécharger la présentation

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. Arrays

  2. Arrays • Most real-world programs handle vast amounts of data. • Fortunately, data usually can be organized and processed systematically. • Arrays are almost always used for this.

  3. Arrays Topics • The idea of arrays. • Array declaration. • Array declaration and construction. • Using arrays. • Automatic bounds checking. • Initializer lists.

  4. What is an array? • An array is an object that is used to store a list of values. • It is made out of a contiguous block of memory that is divided into a number of "slots." • Each slot holds a value, and all the values are of the same type.

  5. Arrays Example • In the example array at right, each slot holds an int. • The name of this array is data. The slots are indexed 0 through 9. • Each slot can be accessed by using its index. • For example, data[0] is the slot which is indexed by zero (which contains the value 23). • data[5] is the slot which is indexed by 5 (which contains the value 14).

  6. Facts • The slots are numbered sequentially starting at zero. • If there are N slots in an array, the indexes will be 0 through N-1. • Sometimes the index is called a subscript. The expression data[5] is usuallypronounced "data-sub-five" as if it were an expression from mathematics: data5.

  7. Using Arrays • Every slot of an array holds a value of the same type. • So, for example, you can have an array of int, an array of double, and so on. • You can have an array of object references. This is discussed in a later chapter. • The array on the left holds data of type int. Every slot contains an int. A slot of this array can be used anywhere a variable of type int can be used.

  8. Using Arrays • For example, data[3] = 99 ; ...works just like an assignment to an int variable. • After it has been executed, the array looks like the picture on the right. • The value in slot 3 of the array has been changed.

  9. Arithmetic Expressions • An expression such as data[3] is called a subscripted variable. • A subscripted variable can be used anywhere an ordinary variable of the same type can be used. • Since data[3] contains an int it can be used anywhere an int variable may be used. • An arithmetic expression can contain a mix of literals, variables, and subscripted variables.

  10. Arithmetic Expressions • For example, if x contains a 10, then (x + data[2]) / 4 evaluates to (10+14) / 4 == 6. • Here are some other legal statements: data[0] = (x + data[2]) / 4 ; data[2] = data[2] + 1; x = data[3]++ ; // the data in slot 3 is incremented data[4] = data[1] / data[6];

  11. Arrays Declaration • Array declarations look like this: type[] arrayName; • This tells the compiler that arrayName will be used as the name of an array containing type. • However, the actual array is not constructed by this declaration. • This declaration merely declares a reference variable arrayName which, sometime in the future, is expected to refer to an array object. • Often an array is declared and constructed in one statetment, like this: type[] arrayName = new type[length ]; • This statement does two things: • It the compiler that arrayName will refer to an array of type. • It constructs an array object containing length number of slots.

  12. Arrays are Objects • An array is an object, and like any other object in Java is constructed out of main storage as the program is running. • The array constructor uses different syntax than other object constructors: new type[length ] • This names the type of data in each slot and the number of slots. • Once an array has been constructed, the number of slots it has does not change. • Here is an example: int[] data = new int[10]; This statement creates the array data and puts a zero into each slot.

  13. Bounds Checking • The length of an array is how many slots it has. An array of length N has slots indexed 0..(N-1) • Indexes must be an integer type. • It is OK to have spaces around the index of an array, for example data[1] and data[ 1 ] are exactly the same as far as the compiler is concerned. • It is not legal to refer to a slot that does not exist: • Say that an array were declared: int[] data = new int[10]; Here are some subscripted variables of this array: data[ -1 ] always illegal data[ 10 ] illegal  (out of bound) data[ 1.5 ] always illegal data[ 0 ] OK data[ 9 ] OK (for the above declaration)

  14. More on Array Declaration • The slots of an array are initialized to the default value for their type. • Each slot of a numeric array is initialized to zero. class ArrayEg1 { public static void main ( String[] args ) { int[] stuff = new int[5]; stuff[0] = 23; stuff[1] = 38; stuff[2] = 7*2; System.out.println("stuff[0] has " + stuff[0] ); System.out.println("stuff[1] has " + stuff[1] ); System.out.println("stuff[2] has " + stuff[2] ); System.out.println("stuff[3] has " + stuff[3] ); System.out.println("stuff[4] has " + stuff[4] ); } }

  15. Using a Variable as an Index • The index of an array is always an integer type. • It does not have to be a literal. • It can be any expression that evaluates to an integer. • For example, the following are legal: int values[] = new int[7]; int index; index = 0; values[ index ] = 71; // put 71 into slot 0 index = 5; values[ index ] = 23; // put 23 into slot 5 index = 3; values[2+2] = values[index-3]; // same as values[4] = values[0];

  16. class ArrayEg2 { public static void main ( String[] args ){ double[] val = new double[4]; //an array of double val[0] = 0.12; val[1] = 1.43; val[2] = 2.98; int j = 3; System.out.println( "slot 3: " + val[ j ] ); System.out.println( "slot 2: " + val[ j-1 ] ); j = j-2; System.out.println( "slot 1: " + val[ j ] ); } }

  17. class ArrayEg3 { public static void main ( String[] args ) { double[] val = new double[4]; val[0] = 1.5; val[1] = 10.0; val[2] = 15.5; int j = 3; val[j] = val[j-1] + val[j-2]; // same as val[3] = val[2] + val[1] System.out.println( "val[" + j + "] == " + val[j] ); } }

  18. Initializer Lists • You can declare, construct, and initialize the array all in one statement: int[] data = {23, 38, 14, -3, 0, 14, 9, 103, 0, -56 }; • This declares an array of int which is named data. • Then it constructs an int array of 10 slots (indexed 0..9). • Finally it puts the designated values into the slots. • The first value in the initializer list corresponds to index 0, the second value coresponds to index 1, and so on. (So in this example, data[0] gets the 23.) • You do not have to say how many slots the array has. • The compiler will count the values in the initializer list and make that many slots. Once an array has been constructed, the number of slots does not change. • Initializer lists are usually used only for small arrays.

  19. class ArrayEg4 { public static void main ( String[] args ) { int[] valA = { 12, 23, 45, 56 }; int[] valB = new int[4]; __________ = ____________ ; __________ = ____________ ; __________ = ____________ ; __________ = ____________ ; } }

  20. class ArrayEg5 { public static void main ( String[] args ) { int[] valA = { 12, 23, 45, 56 }; int[] valB = new int[4]; valB = valA; } }

  21. Facts about arrays • An array is an object that has room for several values, all of the same type. • Each value is stored in a slot of the array. • If there are N slots in the array, the slots are indexed from 0 up to (N-1). • The index must be an integer value (byte, short, or int). • An array declaration looks like: int[] intArray; This declaration declares the array reference intArray. It does not create the actual object.

  22. Facts about arrays 6. An array can be declared and constructed in a combined statement: int[] intArray = new int[17]; This declaration declares the array reference intArray, and constructs an array object containing 17 slots that can hold int. 7. When an array object is constructed using the new operator, the slots are initialized to the default value of the type of the slot. Numeric types are initialized to zero. 8. Once an array object has been constructed, the number of slots it has can not be changed. (However, a completely new array object, with a different number of slots, can be constructed to replace the first array object.) 9. A subscripted variable such as intArray[12] can be used anywhere an ordinary variable of the same type can be used.

  23. Facts about arrays 10. The index used with an array can be stored in a variable, for example int j = 5 ; intArray[ j ] = 24; // same as: intArray[ 5 ] = 24; 11. The index used with an array can be computed in an expression, for example int j = 5 ; intArray[j*2 + 3] = 24; // same as: intArray[ 13 ] = 24; 12. The index used with an array must be within the range 0..(N-1) where N is is number of slots of the array. 13. If an index that is out of bounds is used with an array, an exception is thrown and the program stops running (unless it catches the exception.) 14. An array can be declared, constructed, and initialized using an initializer list. This can only be done when the array is first declared.

More Related