Unformatted text preview:

1ArraysExample• Write a program to keep track of allstudents’ scores on exam 1.• Need a list of everyone’s score• Declare 14 double variables?• What about next semester?Arrays• Declare a list of variables – all with thesame type• Size is determined at time of declarationdouble[] scores = new double[14];Example90.573 87scores:Example//declare the arraydouble[] scores = new double[3];//put 90.5 in the first boxscores:Example//declare the arraydouble[] scores = new double[3];//put 90.5 in the first boxscores[0] = 90.5;90.5scores:2Example//declare the arraydouble[] scores = new double[3];//put 90.5 in the first boxscores[0] = 90.5;scores[1] = 73;scores[3] = 87;90.573 87scores:Alternativedouble[] scores = {90.5, 73, 87};• Initializes elements of the array to valuesgiven when array is createdSubscripts• Subscript describes which box of the array you aredealing with• Array of size N has subscripts– 0, 1, 2, … (n-1)– array of size 3 – 0, 1, 2– subscript added to base address of array• Subscript can be a variable or expression whichproduces an integer– scores[i];– scores[i+5];• If subscript specified does not exist – runtime error(ArrayIndexOutOfBoundsException)Example//assume i = -2;sum = scores[1] + scores[2];sum = scores[1] + scores[i*8];scores[2] = scores[1] + 6;90.573 87scores:Accessing Array Elements• Print all elements of the array scores– Use only one System.out.println statementAccessing Array Elements• Print all elements of the array scores– Use only one System.out.println statementdouble[] scores = {90.5, 73, 82};for(int i = 0; i < 3; i++) { System.out.println("Score " + (i+1) + ": " + scores[i]);}3foreachdouble[] scores = {90.5, 73, 82};for(double d : scores) { System.out.println("Score " + d);}Passing Array Elements• Write a method to add two elements of anarrayPassing Array Elements• Write a method to add two elements of anarray– How is the method called?double add(double num1, double num2) {return (num1 + num2);}Passing Array Elements• Write a method to add two elements of anarray– How is the method called?add(scores[0], scores[1]);…double add(double num1, double num2) {return (num1 + num2);}Passing Arrays• Would like to pass an entire array into amethod– Sum all elements, prompt user for valuesExamplesum(scores);static double sum(double[] scores) { double sum = 0; for(double d: scores) { sum+=d; } return sum;}4Arrays of objectsFlight[] flights = new Flight[10];Multidimensional Arrays• double[][] warmups = new double[14][30];• Declares a multidimensional array with 14rows and 30 columns…01130 1 2 29Example• Set first warmup score for first student to3…01130 1 2 29Example• Set first warmup score for first student to3warmups[0][0] = 3;…01130 1 2 29Example• Print all scores for all students…01130 1 2 29Example• Print all scores for all studentsstatic void printscores(double[][] scores) { for(int row = 0; row < scores.length; row++) { for(int col = 0; col < scores[row].length; col++) { System.out.println("Item " + scores[row][col]); } }}5ArrayList• Like an array, but it can dynamicallychange size• Stores object references– cannot store primitive types without a wrapper• By default, not declared to store aparticular type– ArrayList al = new ArrayList();• Because it is a generic type, we canexplicitly specify the type– ArrayList<String> stringal = new


View Full Document

USF CS 112 - Arrays

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?