Download presentation
Presentation is loading. Please wait.
1
Java SE 7 One and Multi Dimensional Arrays Module 6
Programming with Arrays One and Multi Dimensional Arrays Welcome to the video tutorial on one and multi dimensional arrays in Java.
2
Arrays Review Series of items of the same type
Each item is known as an element Items can be accessed by an index; Index starts at 0 Elements can be made from primitive types or objects Initialized with new and then with the data type int [] daysofweek = new int[7]; Arrays can be one or multi dimensional Size is denoted by the number in brackets int [] fruit = new int[10];// size of array is 10 Size once set cannot be changed In chapter 4, we learned that Java arrays are linear, homogeneous data structure where all the elements are of the same data type. They allow the developer to use one variable with one or more indexes to access multiple independent pieces of data. Arrays can be used to store both primitives and objects. Elements in an array are accessed using an index. Arrays may be one dimensional or multi-dimensional.
3
One dimensional array arrayOfInts[0] arrayOfInts[1] arrayOfInts[2] 10
20 Here’s a basic representation of a one dimensional array. A one-dimensional array uses only a single index.
4
Declaration and Initialization
// Declaring an array of integers int[] arrayOfInts; // Define the size of the array and initialize the array int[] arrayOfInts = new int[3]; //Initialize elements in the array arrayOfInts[0] = 10; arrayOfInts[1] = 20; arrayOfInts[2] = arrayOfInts[0]; Here’s an example of a one dimensional integer array declaration. In the first step, we declare a variable named arrayofints to be an integer array. This is done by adding box brackets to the end of the data type. The box brackets after the data type means it will be an array. The box brackets should follow the data type, but note that it is also valid for them to follow the variable name instead, which is the method preferred by most developers. Standard Java coding conventions suggest they should be used only with the data type. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type. The second step allocates memory for an array of 3 integers. The number in brackets on the right indicates the size of the array. In this example, the array has three items. Each item is of type int. Recall that, regardless of whether the new array is of primitives or objects, it must be initialized with NEW and then with the data type. In the last step we initialize each element in the array to an integer value. The value can be a constant or a referenced value.
5
Accessing elements and Methods
Sample array: String[] clockTypes = {"Wrist Watch","Desk Clock","Wall Clock"}; Example1: Accessing an element System.out.println(clockTypes[1]); //Output: Desk Clock Example 2: Print length (size) of array System.out.println("length: " + clockTypes.length); //Will return 3 One-dimensional arrays are very straightforward to use. Once declared and initialized, each element from the array can be accessed by using its corresponding index. Every object or primitive has an index number associated with it. It is important for you to remember that an array with a length of three has the indexes 0, 1, and 2. The first index is always 0. Let’s look at some things we can do with one dimensional arrays: We can access and print out any element in the array. For example, System dot out dot printline, clocktypes of 1 will print out the second element in the array which is Desk clock. Secondly, remember that arrays are objects. This array is made up of String objects. When the array is used with an index number, it behaves just as a String object variable would. For example, you can access the public length field to obtain the length of the array. So, when you print out clocktypes.length it would return a value of 3, which is the size or length of this array. Java also has built-in methods for copying arrays. These methods copy the data and create two independent arrays upon completion. The arraycopy method is a static method that is part of the System class. When using this method, you must ensure that you do not go outside the bounds of either array. The destination array must also be declared as the same type and initialized. In this example, the last two elements from clockTypes are copied into another array called newClockTypes. The two arrays are independent. If values in one are modified, it will not affect the other array.
6
Copying Arrays Sample array:
String[] clockTypes = {"Wrist Watch","Desk Clock","Wall Clock"}; Example 3: // Copy an array String[] clockTypes = {"Wrist Watch","Desk Clock","Wall Clock"}; String[] newClockTypes = new String[2]; System.arraycopy(clockTypes, 1, newClockTypes, 0, 2); Java also has built-in methods for copying arrays. These methods copy the data and create two independent arrays upon completion. The arraycopy method is a static method that is part of the System class. When using this method, you must ensure that you do not go outside the bounds of either array. The destination array must also be declared as the same type and initialized. In this example, the last two elements from clockTypes are copied into another array called newClockTypes. The two arrays are independent. If values in one are modified, it will not affect the other array.
7
Multi dimensional arrays
More than one index An array whose components are themselves arrays JVM limit is 256 Virtually no limit to number of dimensions Multi-dimensional arrays have more than one index. A multi-dimensional array with two dimensions, or indexes, is an array of arrays. An array can have three, four, or more dimensions. The Java language specification does not place a limit on the number of dimensions that an array can have. However, the Java virtual machine (JVM) specification does place a practical limit of 256 dimensions.
8
Declaration // Two-dimensional array being declared both ways
String[][] chessBoard; String chessBoard[][]; // Three-dimensional array being declared both ways int[][][] cube; int cube[][][]; // Four-dimensional array being declared both ways int[][][][] quad1; int quad1[][][][]; A multi-dimensional array is declared similarly to a one-dimensional array, with additional square brackets for each dimension. Where a one-dimensional array has one square bracket following the type or variable name, a two-dimensional array has two brackets, a three-dimensional array would have three, and so on, as shown in the examples.
9
Initialization // Using the new operator
String[][] square = new String[2][2]; int[][][] cube = new int[3][3][2]; int[][][] array3D = new int[2][][]; array3D[0]= new int[5][]; // Using curly brackets with array values String[][] square = {{"1","2"},{"3","4"}}; int[][] oddSizeArray = {{1,2},{1,2,3,4}, {1,2,3}}; Initializing Multi-Dimensional Arrays Multi-dimensional arrays can be initialized similarly to one-dimensional arrays. They are either initialized with the new operator as shown, or they use curly brackets with the values to be stored in the array. When using the new operator to initialize an array, notice that the size of each dimension does not have to be defined. Because a multi-dimensional array is just an array of arrays, it is okay to define the length of only the first dimension. In the second set of examples, the curly bracket groups are separated by commas. The values inside the curly brackets represent an array. You can think of multi-dimensional arrays as an array of arrays. This is because the first level is an array that contains an array at each element. Note that the arrays that are contained do not all have to be the same length.
10
Accessing array elements
Sample string: String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {“Wright", “Cooper"} }; Accessing elements: System.out.println(names[0][0] + names[1][0]); System.out.println(names[0][2] + names[1][1]); Output: Mr. John Wright Ms. Betsy Cooper Multi-dimensional arrays are used similarly to one-dimensional arrays, except that additional dimensions need to be accounted for. When an element is accessed, an index must be provided in square brackets for each dimension as shown in the example.
11
Summary Pros Cons One or more dimensions Can store primitives
No overhead to access data Easy to create multi-dimensional arrays. Cons Cannot be resized When adding or removing data, the array must be managed manually To summarize, arrays can be one dimensional or multi dimensional. Once declared and initialized, each element from the array can be accessed by using its corresponding index. Arrays are also objects. Methods that are applicable to a given object type can be used by arrays as well, as we saw with our length and arraycopy functions. The advantages of arrays are that they can store primitives, they are easy to create regardless of the number of dimensions, and there is no overhead to access data. The major disadvantages are that their size is fixed regardless of usage, and the process to add or remove data from an array is manual.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.