Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
for
loops to traverse and process array dataAn array is an object that can store many values of the same type. Making an array requires declaring how many values the array can hold, and what type of values the array will hold. The array type and capacity cannot change once the array has been made.
// makes an integer array with 4 slots in it. Default: all values are 0.
int[] arrayName = new int[4];
// another way to make an array: makes an integer array with 4 slots, all values being 0
int[] arrayName = {0, 0, 0, 0};
// arrays can be of any type! // when object arrays are made, default value of each slot is null. String[] arrayName = new String[5];
An array can be thought of as a series of cubbyholes. We can put values in and take values out of the cubbyholes by specifying which cubbyhole we want to work with. Just like with Strings, each cubby is indexed from 0 to the the length of the String/array.
int[] a = new int[5];
// notice that unlike with Strings, we do not put a () after length
int length = a.length;
int[] a = new int[10]; // makes the value at index 5 equal to 8. // the pre-existing value at index 5 is ignored and overwritten. a[5] = 8; // sets num equal to 8, the value at index 5 of a. Does not change the value of a[5]. int num = a[5];
ArrayIndexOutOfBoundsException
If you try to access an index smaller than 0 or greater than array.length - 1
, your program will crash with an ArrayIndexOutOfBoundsException
.
int[] a = new int[4]; // changes every value in array to equal 5. for (int i = 0; i < a.length; i++) { a[i] = 5; } // causes program to crash with ArrayIndexOutOfBoundsException a[6] = 5; // also causes program to crash with ArrayIndexOutOfBoundsException int num = a[4];
An array is an object.
Whether the elements of the array are objects or not depends on what type of values the array stores.
int
s, double
s, or char
s), then the elements are not objects.Scanner
s or PrintStream
s), the array elements are objects.
int x = 5;
int y = x;
x = 10;
// here, x = 10, y = 5
Setting y = x
meant y =
the value that x had at that point in time. y
does not change values when x
changes values.
int[] a = new int[5]; // a ==> [0, 0, 0, 0, 0] int[] b = a; // a ==> [0, 0, 0, 0, 0] <== b a[0] = 10; // a ==> [10, 0, 0, 0, 0] <== b b[1] = 8; // a ==> [10, 8, 0, 0, 0] <== bSetting
b = a
means b
and a
now reference the same array. Changes to either a
or b
change both a
and b
.
Fill in the array with the values that would be stored after the code executes:
int[] data = new int[8]; data[0] = 3; data[7] = -18; data[4] = 5; data[1] = data[0]; int x = data[4]; data[4] = 6; data[x] = data[0] * data[1];
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
value | 3 | 3 | 0 | 0 | 6 | 9 | 0 | -18 |
public static returnType methodName(arrayType[] parameterName) { // pass array parameter
public static returnArrayType[] methodName(parameters) { // return array
double
s, and returns a new array of rounded int
s:
public static int[] roundAll(double[] realNums) { int[] roundedNums = new int[realNums.length]; for (int i = 0; i < realNums.length; i++) { roundedNums[i] = (int) Math.round(realNums[i]); } return roundedNums; }
roundAll
method from the previous slide:
import java.util.*; // to use Arrays
public class MyProgram {
public static void main(String[] args) {
double[] realNumbers = {5.5, 7.31, 8.09, -3.234234, 2.0, 0.0};
int[] roundedNumbers = roundAll(realNumbers);
System.out.println(Arrays.toString(roundedNumbers));
}
...
}
// Output: [5, 7, 8, -3, 2, 0]
Note: to print out the array we said System.out.println(Arrays.toString(arrayName))
. Why's that? If we just say System.out.println(arrayName)
, we'd get a weird jumble of letters and numbers. To get a well formatted array, we need to ask the Arrays
class to turn the array into a nice String for us!
Arrays
class methodsArrays
class has a ton of useful methods!
Method name | Description |
---|---|
Arrays.binarySearch(array, value)
|
returns index of value in a sorted array (< 0 if not found) |
Arrays.copyOf(array, length)
|
returns a new copy of an array |
Arrays.equals(array1, array2)
|
returns true if the two arrays contain same elements |
Arrays.fill(array, value)
|
sets every element to the given value |
Arrays.sort(array)
|
arranges the elements into sorted order |
Arrays.toString(array)
|
returns a string for the array, such as "[10, 30, -25, 17]" |
It depends!
int[] a
in methodX()
and then pass a
into methodY()
, any changes made to the parameter array in methodY()
will also be reflected in methodX()
.
methodX()
calls methodY()
, and then we make int[] a
in methodY(), a
must be returned to methodX()
for methodX()
to be able to use it.
yes
or no
:
// takes in an integer array, adds 5 to each element public static ??? addFive(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = a[i] + 5; } return a?no}
// takes in an integer array, returns an array with each element increased by 5. // does not modify given array. public static ??? addFive(int[] a) { int[] returnArray = new int[a.length]; for (int i = 0; i < a.length; i++) { returnArray[i] = a[i] + 5; } return returnArray?yes}
Nice job making it this far--labs are tough! Feel free to work with the person next to you for the remaining slides. Labs are a unique opportunity (unlike homework) to collaborate directly on ideas, and practice peer programming.
These next problems get a little more challenging as we explore earlier concepts further.
We put a lot of problems in here so that you have plenty to refer back to later when working on homework. Don't feel bad if you don't finish all of them--Brett can't finish them all in a 50 minute lab, either! :)
Forest the cat says good job!
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!