[an error occurred while processing this directive] CSE 142 Lab 7: Arrays

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

Today's lab

Goals for today:

[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) {   // pass array parameter
public static returnArrayType[] methodName(parameters) {            // return array
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)

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 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!