Download presentation
Presentation is loading. Please wait.
Published byΑκακαλλις Κορομηλάς Modified over 6 years ago
1
Arrays in Java What, why and how Copyright Curt Hill
2
What is an array? Multiple occurrences of values of one type
One name many values Item is selected at run time by an integer index A true data type in Java, unlike C++ Always an object Always dynamic The mark of arrays is a pair of square brackets: [ ] Copyright Curt Hill
3
Why do we need arrays? We often need to select a value by an index rather than just name If a picture has 240,000 pixels, do we really want 240,000 different variable names? Instead we find one name and use an integer subscript to choose which of the many we want Copyright Curt Hill
4
Counting For Example static void process(Picture p){ // Enhance red
Pixel [] pixels = p.getPixels(); for(int i=0;i<pixels.length;i++){ Pixel ap = pixels[i]; int r = 3 *ap.getRed()/2; ap.setRed(r); } Copyright Curt Hill
5
Explanation A reference to pixels either refers to the whole object:
Such as pixels.length which describes how many Otherwise a bracket selects which one: pixels[i].setRed(red); Set the ith pixel’s red value Copyright Curt Hill
6
For All Example static void process(Picture p){ // Enhance red
Pixel [] pixels = p.getPixels(); for(Pixel ap: pixels){ int r = 3 * ap.getRed()/2; ap.setRed(r); } Copyright Curt Hill
7
Explanation This version of the for does two things for us
It infers the length of the array It hides the subscripting of the pixel Copyright Curt Hill
8
Arrays One name, many values
Arrays elements demonstrate identical form and function The form is that each element has exactly the same type Polymorphism can come into play here, but not considered further until much later The function is that they are there for the same purpose Copyright Curt Hill
9
What is needed? Declaration Allocation Access and reference
Marked by the [] Allocation Determines the size Access and reference Indexing or subscripting Answers to these questions: What can we do to an array element? What can we do to the whole array? Copyright Curt Hill
10
Array declaration Two similar declarations:
int [ ] a, b, c[ ]; int d[]; Brackets may precede or follow the variable name No size is allowed in declaration Brackets are attached to variable name b is not an array, but a simple integer Even an array of primitives is an object Copyright Curt Hill
11
Array Allocation Two step for objects or primitives
Allocate the array handle This determines the size of the array Allocate (or initialize) the individual elements Primitives need to be initialized Objects are usualy initialized by the allocation In either case, usually done with a for Copyright Curt Hill
12
Preface to Allocation At least for a time we will get an array from somewhere else A picture We will not use the new operator on it, nor will we initialize it by accessing individual elements We can create and allocate arrays with new The technique is seen in next slides Copyright Curt Hill
13
Allocation examples Two statements One statement
int x[]; x = new int [20]; One statement int a[] = new int[20]; The initialization of both of these still needs to be done Copyright Curt Hill
14
Using whole arrays Suppose the following declaration
int [] a = new int [5], b[]; Without subscript an array name is just an object handle b = a; Have both handles refer to same array a b Copyright Curt Hill
15
Subscripting Using a subscript an element is obtained
a[k] = a[j+2]-2; Anywhere an int can be used a[k] can be used First element has subscript zero Last element has subscript size minus one Copyright Curt Hill
16
A Subscript The item in brackets chooses which element of array to be used The item may be a integral constant integral variable integral expression No doubles or float subscripts There is no element between [2] and [3] Must be in the correct range 0 to length of array-1 Copyright Curt Hill
17
Subscript Errors Suppose: int [] a = new int [10]; . . . a[k] = 2;
If k is less than zero or greater than 9 a subscript error occurs ArrayIndexOutOfBoundsException is thrown It is the programmers responsibility to guarantee that an index is in the correct bounds Copyright Curt Hill
18
Subscript Traceback Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2 at pd1.PictureDemo1.process(PictureDemo1.java:23) at pd1.PictureDemo1.main(PictureDemo1.java:16) ArrayIndexOut OfBounds is the error The -2 is the actual subscript Find the first (from the top) line that is in your code. In this case it is line 23 Copyright Curt Hill
19
Discussion Since an array is one name with many values, an array element has a two piece name The array name and the subscript We may assign to this name or use in an expression Processing an array sequentially is most often done with a for statement Copyright Curt Hill
20
Array extras Arrays can be parameters or results of methods
Lowest subscript is always zero The number of elements of an array can be accessed by a property: a.length One larger than largest valid subscript Copyright Curt Hill
21
The questions What can we do to an array element?
Anything reasonable to do to a variable of the same type What can we do to the whole array? Pass as a parameter Return as a result Assign the handle Get the length Copyright Curt Hill
22
Array Handles Like any object what you have is the handle
This handle can be reused int [] ar = new int[20]; … ar = new int[40]; Thus the length of the array is dependent on the current version A different length depending on where it is checked Copyright Curt Hill
23
Tracing Somewhat problematic
Since one name has many values we have to be careful to keep track of them properly Moreover, we do not know how many elements there are until the allocation Copyright Curt Hill
24
Discussion A primitive passed to a method just passes a copy
The original cannot be changed An object just passes a handle The object may changed but not the handle Arrays may thus have their values changed Strings are the exception The string is immutable Copyright Curt Hill
25
Conclusions Arrays are a valuable means to structure data
They are an object without an object name They require a subscript (or index) to access individual elements Methods may change array parameters Multiple dimension arrays are also possible but will be discussed later Copyright Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.