1 / 31

Array Basics

Array Basics. Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would you? An array is a collection of data values of the same data type.

tyson
Télécharger la présentation

Array Basics

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. Array Basics • Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? • You can, but would you? • An array is a collection of data values of the same data type. • If your program needs to deal with 100 integers, 365 real numbers, etc., you will use an array.

  2. Data Type reference primitive long String byte short Applet MessageBox double char InputBox int HiLo boolean etc. float Arrays Variables are Reference Variables • Primitive variables contain values • Reference variables point at objects

  3. 0 1 2 3 4 5 6 7 8 9 10 11 rainfall Primitive Arrays • A primitive array stores multiple values of the same primitive data type. The index of the first position in an array is 0.

  4. double[] rainfall; rainfall = new double[12]; double rainfall [ ]; rainfall = new double[12]; Variation 1 Variation 2 An array is like an object! Arrays of Primitive Data Types • Array Declaration <data type> [ ] <variable> //variation 1 <data type> <variable>[ ] //variation 2 • Array Creation <variable> = new <data type> [ <size> ] • Example

  5. 0 1 2 3 4 5 6 7 8 9 10 11 rainfall double[] rainfall = new double[12]; This indexed expression refers to the element at position #2 rainfall[2] Accessing Individual Elements • Individual elements in an array accessed with the indexed expression.

  6. Example Programs • Zadoot … out to reality … ArrayElements1.java • Zadoot … out to reality … ArrayElements2.java

  7. double[] rainfall = new double[12]; double annualAverage; double sum = 0.0; int index; for (index = 0; index < rainfall.length; index++) { rainfall[index] = keyboard.nextDouble(); sum += rainfall[index]; } annualAverage = sum / rainfall.length; The public constant length returns the capacity of an array. Array Lengths • Phrrud … out to reality … ArrayRain.java • Phrrud … out to reality … ArrayAverages.java

  8. Array Bounds Errors • Trying to access an array element that does not exist causes a runtime error • Negative indices • Indices beyond the size • Falop … out to reality … ArrayBoundsError.java

  9. number.lengthsamplingData.length 4 9 Array Initialization • Like other data types, arrays can be declared and initialized at the same time. int[] number = { 2, 4, 6, 8 }; double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00, 3.123, 22.084, 18.08 }; • The capacity of the array is set to the number of elements in the list. • Groeeet … out to reality … ArrayInit.java • (Secretly, Java pre-initializes arrays to the default value for the type, e.g., 0 for int arrays.)

  10. References are Pointers • A reference variable points to an object • So, arrays are like objects, but don't worry about that for now – we’ll come back to it. • But it does mean you can • Lose an array • Have an array variable pointing nowhere • Have multiple references to an array • Not copy an array with =

  11. customer Array in Memory Array in Memory A B A. The variable is allocated in memory. int[] customer; customer = new int[5]; B. The reference to the new array is assigned to customer. customer = new int[5]; C C. The reference to another array overwrites the reference in customer. Losing an Array State of Memory Code

  12. Garbage Collection • An array that has no references is garbage collected by the java program • Spaaocie … out to reality … ArrayGC.java

  13. The array to no-where • An array variable can be explicitly made to point to no data, using the null value • A null array is different from an array of length zero. • Spaaocie … out to reality … ArrayNULL.java

  14. clemens twain Array in Memory A B A. Variables are allocated in memory. int[] clemens, twain; clemens = new int[5]; B. The reference to the new array is assigned to clemens. twain = clemens; C C. The reference in clemens is assigned to customer. Having Two References to an Array State of Memory Code • Dooop … out to reality … ArrayDup.java

  15. Cloning an Array • An array can be copied using the clone method • It's necessary to cast the clone to the right array type • Babababoom … out to reality … ArrayClone.java

  16. Two-Dimensional Arrays • Two-dimensional arrays are useful in representing tabular information.

  17. double[][] payScaleTable; payScaleTable = new double[4][5]; Declaring and Creating a 2-D Array • Declaration • <data type> [][] <variable> //variation 1 • <data type> <variable>[][] //variation 2 • Creation • <variable> = new <data type> [<size1>][<size2>] • Example 0 1 2 3 4 0 1 2 3

  18. Example Programs • Ieeei … out to reality … ArrayMatrix.java • Ieeei … out to reality … ArrayCalendar.java • Ieeei … out to reality … ArrayCube.java

  19. Multi-dimensional Arrays … NOT • Java does not really have multi-dimensional arrays • Java has arrays of arrays • int[][] data = new int[3][5]; • is shorthand for • int[][] data = new int[3][]; • data[0] = new int[5]; • data[1] = new int[5]; • data[2] = new int[5];

  20. Multi-dimensional Arrays in RAM • int[][] data = new int[3][5]; • Zuuu … out to reality … ArrayRAMMatrix.java • Zuuu … out to reality … ArrayRAMCube.java

  21. Irregular Arrays • int[][] weirdData = new int[3][]; • weirdData[0] = new int[5]; • weirdData[1] = new int[4]; • weirdData[2] = new int[7];

  22. Irregular Arrays • int[][] weirdData = new int[3][]; • weirdData[0] = new int[5]; • weirdData[1] = new int[4]; • weirdData[2] = new int[7]; • weirdData.length == 3 • weirdData[1].length == 4 • Jioooul … out to reality … ArrayIrreg1.java

  23. Irregular Arrays … null • int[][] data = new int[3][]; • data[0] = new int[5]; • data[1] = new int[4]; • data[2] = null; • Jioooul … out to reality … ArrayIrreg2.java

  24. Multi-dimensional Array Initialization • Like 1D arrays, it is possible to declare and initialize a multi-dimensional array at the same time. • Frong … out to reality … ArrayMDInit.java • Frong … out to reality … ArrayCubeInit.java

  25. At before searchMinimum A arrayOne Passing Arrays to Methods - 1 Code public int searchMinimum(float[] number)) { … } A minOne = searchMinimum(arrayOne); A. Local variable number does not exist before the method execution State of Memory

  26. B The address is copied at B arrayOne arrayOne number Passing Arrays to Methods - 2 Code public int searchMinimum(float[] number)) { … } minOne = searchMinimum(arrayOne); B. The value of the argument, which is an address, is copied to the parameter. State of Memory

  27. C While at inside the method C arrayOne number Passing Arrays to Methods - 3 Code public int searchMinimum(float[] number)) { … } minOne = searchMinimum(arrayOne); C. The array is accessed via number inside the method. State of Memory

  28. D At after searchMinimum D arrayOne arrayOne number Passing Arrays to Methods - 4 Code public int searchMinimum(float[] number)) { … } minOne = searchMinimum(arrayOne); D. The parameter is erased. The argument still points to the same object. State of Memory

  29. Example Programs • Flunot … out to reality … ArrayParamAvg.java • Flunot … out to reality … ArrayParam1.java

  30. Returning Arrays • Array variables in methods exist until the method ends, but the array data lives while referenced • An array variable can be returned from a method • The receiving array variable then refers to the array data, and the array persists • Wrrbbrack … out to reality … ArrayParam2.java

  31. Local arrays • Array variables in methods exist until the method ends • The array data referred to by such an array variable is lost and garbage collected when the method ends • Dessserts … out to reality … ArrayLocalGC.java

More Related