University of Washington, CSE 142
Lab 7: Arrays
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
Basic lab instructions
-
Mouse over highlighted words if you're not sure what they mean!
-
Talk to your classmates for
help.
-
You may want to bring your textbook to future labs
to look up syntax and examples.
-
Stuck? Confused? Have a question? Ask a TA for
help, or look at the book or past .
-
Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
-
Feel free to complete problems in any order.
-
Make sure you've signed in on the sign-in sheet before you leave!
Today's lab
Goals for today:
- use arrays for storing lists of data
- practice using
for
loops to traverse and process array data
- pass arrays as parameters and returns from methods
- explore the idea of reference parameters
- Where you see this icon, you can click it to check the problem in Practice-It!
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
Arrays as parameter/return (declare)
public static returnType methodName(arrayType[] parameterName) {
public static returnArrayType[] methodName(parameters) {
-
Arrays can be passed as parameters and returned from methods.
-
This method takes an array of
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;
}
Arrays as parameter/return (call)
-
Below is an example usage of the
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));
}
...
}
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 methods
In fact, the
Arrays
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]"
|
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
If you finish them all...
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!