Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];

Similar presentations


Presentation on theme: "Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];"— Presentation transcript:

1 Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100]; The size may be a variable or an expression. However, once the array is created, we cannot change it size.

2 Array Initialization An array variable can be initialized, similar to other variables. The notation is to put the values between curly braces, separated by commas. For example, int[] grades = { 60, 95, 70, 92, 100, 65 };

3 Length Every array has a length variable, whose value is the length or size of the array. This is especially useful when an array is initialized to a list of values. For example, grades.length in the previous example would have the value 6. This field can be used, but not changed.

4 Partially Filled Arrays Just because an array has 100 elements, doesn't mean that the program will have to use (or even set) all of them. For example, in our standard deviation program, we created an array with 100 elements, but used a sentinel-controlled loop to read in the grades. The user could type in fewer (but not more) than 100 grades. For this reason, we also had a counter, num, to count the actual number of grades.

5 Summary  Arrays can hold more than one value  All the values must be of the same type  Values are referenced using a index ( int )  Array indices start at 0 and go to length-1  It is an error to try to access a value outside the bounds of an array  Arrays are objects, and must be created using the new operator

6 Arrays as Objects Arrays are objects and array variables are object variables. If an array variable is equal to another array variable in an assignment statement, the reference is copied, and both variables point to the same data object. Change one changes the other.

7 Example int[] a = new int[100]; a[0] = 5; a[1] = 10; a[2] = 7; int[] b = a; b[0] = 25; System.out.println(a[0] + " " + a[1] + " " + a[2]); would print out 25 10 7

8 Passing Arrays to Methods Passing an element of an array to a method, e.g., grades[0], is the same as passing any value of the base type (in this case, int ) to the method. You can also pass the entire array to a method. The method parameter specifies the type of the parameter, i.e., int[], but not the size, since the size is a property of the data object itself, and not the type. Like assignment, the reference is copied, and the parameter refers to the data object.

9 Example public static void bubbleSort(int[] a, int n)... public static void main(String args[]) {... bubbleSort(grades, num); The parameter a is initialized to refer to the same data object as the variable grades in main. Changing the values of a, changes the values of grades.

10 More on Arrays as Objects Since arrays are objects, the equality operator, ==, checks if two arrays are the same data object, not whether or not they have the same values. If the latter is what you want, then you'll need a loop to compare the values. An array can be a return type of a method. If it is, and the array is declared private, the user can still change the values by using the returned object.


Download ppt "Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];"

Similar presentations


Ads by Google