1 / 19

Java Unit 9: Arrays

Java Unit 9: Arrays. Declaring and Processing Arrays. What are arrays?. Arrays are ordered collections of data items that are all of the same type. Using subscript notation an array with the name a would take this form : (a 0 , a 1 , …, a i , …, a n-1 ). What are arrays?.

Télécharger la présentation

Java Unit 9: 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. Java Unit 9: Arrays Declaring and Processing Arrays

  2. What are arrays? • Arrays are ordered collections of data items that are all of the same type. Using subscript notation an array with the name a would take this form: • (a0, a1, …, ai, …, an-1)

  3. What are arrays? • Each element of the array could contain a value, such as a double. • The elements are identified by the order in which they appear in the array. • In computer science, it is customary to start with 0.

  4. Constructing arrays • Arrays in Java are objects, constructed like this: • double[] myArray;myArray = new double[10]; • In this declaration, ‘double’ is the type of value that all of the elements in the array will be. • The brackets after ‘double’ indicates that this variable is an array. • In the assignment, we use the keyword new, followed by the type with brackets. In the brackets, the number of elements in the array is defined. In this case, it’s 10. • You could also say the array has a size of 10.

  5. Constructing arrays • As usual, you can declare and assign in a single line. • double[] myArray = new double[10];

  6. Accessing arrays • Once an array has been constructed, it’s elements can be accessed using an integer index value in square brackets. • The element of the array, a subscripted variable, can be used like any other variable that is the same type as the array. • myArray[0] = 25.2;double myVar = myArray[0];System.out.println(myArray[0]);

  7. Array indexing • Arrays start at the 0th element. • So if an array of size 10 is declared, it’s indexing runs from 0 to 9. • Ignoring element 0 may or may not be a mistake, but you can not access myArray[10], because it goes outside the array. • It’s similar to how a string object indexes its characters.

  8. Array Types • It is possible to declare arrays containing simple types and object references: • int[], double[], boolean[], String[] • Although local variables do not have initialization in general, array contents have default initialization regardless of where they are declared or how they are used. • Numerics are 0 by default • Booleans are false by default • Object references are null by default

  9. main(String[] args) • Consider this line of code that appears in every program: • public static void main(String[] args) • We can see that it takes an array of Strings as a parameter. • If this program is run from the command line, multiple command line arguments can be entered on the same line that executes the program, with each argument separated by a string: • java MyJava.class myArg1 myArg2 myArg3 • The command line arguments would be stored inside an array, and passed into the main() method as ‘args’.

  10. Arrays and Objects • An array can be declared to contain objects of any kind of object. • Cup[] myCups = new Cup[10]; //An array of 10 Cup object references • As usual, the class name is capitalized. • The name of the array is a reference to a constructed object, so it is not capitalized. • If we index a single element in the object array, we can call methods on them like any other object. • intmyCount = myCups[3].getSeedCount();

  11. Arrays and Loops • Loops are incredibly useful in processing arrays. • Since the elements of arrays can be identified by consecutive integers, it is a simple matter to use the loop index as the array index: • for(int j = 0; j < 10; j++) {myArray[j] = j * j;} • The initial value of j is 0, the beginning element of the array. • The last value of j is 9, so it stops before it refers to a non-existent element. • We could have written the looping condition as j <= 9, but there is a logic to using strict inequality and the size of the array, explained on the next slide.

  12. Arrays and Loops • Because the size of arrays is needed so often, Java has provided the means to obtain it: • myArray.length • This returns the number of elements for that array • Note that in this case there is no parenthesis, nor is there a method called getLength() • ‘length’ is actually a ‘public final’ instance variable. We can access this variable because it is public. • Using the size of the array itself instead of a hard-coded value, processing the entire array becomes this: • for(int j = 0; j < myArray.length; j++) {myArray[j] = j * j;}

  13. Arrays and Loops • Arrays can be traversed in loops in either direction. • To access the array from the last element to first, the starting and stopping variables of the index have to be set accordingly. • for(int j = myArray.length – 1; j >= 0; j--) {…}

  14. Arrays are fixed in size • It is important to note that just like with any other data item, an array can only be declared once. • In that one declaration, its size, the number of elements in the array is fixed. • The size cannot be changed later on during the course of a program run. • This makes it necessary to anticipate the largest number of elements that might ever be needed and declare the size of the array accordingly. • This may mean that a large number of positions in the array will remain unused.

  15. Multi-dimensional arrays • Arrays of more than one dimension can be declared. • Two dimensional arrays, or matrices, can be represented as a table of values, which is m x n in size. • For both dimensions, indexing starts at 0 as usual. • This means that indexing goes up to m – 1 for the rows, and n – 1 for columns.

  16. Multi-dimensional arrays • The first subscript of an element represents its row and the second represents its column. • The declaration of such an in Java, if it contained integer elements and 4 rows and 5 columns, would take this form: • int[][] myIntArray = new int[4][5]; • The parts of the declaration of this array are analogous to the corresponding parts for a one dimensional array. The only difference is that two sets of square brackets are used, the first indicating the rows, and the second the columns.

  17. Accessing Multi-dimensional arrays • Individual elements are accessed by specifying the index values for the row and column in square brackets. For example: • myIntArray[2][4] = 95; • Two dimensional arrays can be processed conveniently using nested for loops. Just as with one dimensional arrays, array indexing starts at 0 and the lengths of the dimensions can be used to set limits on the loop indexes. This introduces a slight complexity.

  18. Accessing Multi-dimensional arrays • for(int i = 0; i < myIntArray.length; i++) { for(intj = 0; j < myIntArray[0].length; j++) { … }} • myIntArray.length gives the number of rows in myIntArray • myIntArray[0].length gives the number of columns in the 0th row (or any row) of myIntArray

  19. Accessing Multi-dimensional arrays • A two dimensional array is really a one dimensional array of references to a set of one dimensional arrays. In other words, it’s an array of arrays. • The value of the length returned for the name of the array alone is the number of rows, or the number of one dimensional arrays it contains. • If the name of a two dimensional array is given with a single set of brackets, this represents a single row. • The length of one row is the number of columns in the two dimensional array. • Syntactically, the use of square brackets to specify the dimensions is analogous to the two dimensional case. • The underlying reality is that a three dimensional array is an array of arrays of arrays, and so on for higher dimensions.

More Related