Array Processing

21
Array Processing

description

Array Processing. Demonstrate processing entire arrays using loops. Demonstrate Initializer lists. Demonstrate how to use the length attribute to control array processing. Common need to process every element in an array using a loop. Adding/averaging array elements. - PowerPoint PPT Presentation

Transcript of Array Processing

Page 1: Array Processing

Array Processing

Page 2: Array Processing

•Demonstrate processing entire arrays using loops.•Demonstrate Initializer lists.•Demonstrate how to use the length attribute to control array processing.

Page 3: Array Processing

•Common need to process every element in an array using a loop.

• Adding/averaging array elements.• Linear searching of an array.

•Technique involves looping while iterating a variable to take on values between 0 and n-1.

Page 4: Array Processing

public class ArrayProcessing1 { public static void main(String[] args) { double[] numbers = new double[3]; double total = 0.0; numbers[0] = 2.5; numbers[1] = 3.5; numbers[2] = 6.0; //Sum elements in array for( int i = 0; i < 3; i++ ){ total += numbers[i]; } System.out.println("Total=" + total); }

Page 5: Array Processing

public class ArrayProcessing2{ public static void main(String[] s) { double[] numbers = new double[3]; double total = 0.0; numbers[0] = 2.5; numbers[1] = 3.5; numbers[2] = 6.0; //Sum elements in array for( double num:numbers ){ total += num; } System.out.println("Total=" + total); }

Page 6: Array Processing

•Initializer lists (numbers contained between braces) can be used to initialize arrays. •If you provide an initializer list with fewer elements than the size of the array, those not provided with an initializer default to 0.

Page 7: Array Processing

public class ArrayProcessing3 { public static void main(String[] args) { double[] numbers = {2.5, 3.5, 6.0 }; double total = 0.0; //Sum elements in array for( int i = 0; i < 3; i++ ){ total += numbers[i]; } System.out.println("Total=" + total); }}

Page 8: Array Processing

//Reset only negative array values to 0.0

public class ArrayProcessing4{ public static void main(String[] s) { double[] numbers = {4.5, -7.0, 5.5, 24.5 }; for( int i = 0; i < 4; i++ ){ numbers[i] += 0.1; System.out.print(“ “ + numbers[i]);; } }}

Page 9: Array Processing

public class ArrayProcessing6 { public static void main(String[] args ) { double[] numbers = {4.5, -7.0, 5.5, 24.5 }; double[] saveNumbers = new double[numbers.length];

for( int i = 0; i < numbers.length; i++ ){ saveNumbers[i] = numbers[i]; } for( int i = 0; i < numbers.length; i++ ){ System.out.println( numbers[i] + "->" + saveNumbers[i] ); } }}

This is an example. There are utilities to do this for you.

Page 10: Array Processing

•Previous examples used constants (e.g. 4) to limit the loop. If the size of the array changes then this value will have to change.•Arrays are objects which have a length field.•Using this field will result in fewer long term maintenance. Change the for loop as below. for( int i = 0; i < numbers.length; i++ ){ total += numbers[i]; }

Page 11: Array Processing

//Double each array value in place then print

public class ArrayProcessing5 { public static void main(String[] args ) { double[] numbers = {4.5, -7.0, 5.5, 24.5 }; double total = 0.0; for( int i = 0; i < numbers.length; i++ ){ numbers[i] *= 2.0; } for( int i = 0; i < numbers.length; i++ ){ System.out.println( "Value at cell " + i + " is " + numbers[i]); } }}

Page 12: Array Processing

Your Turn!

Page 13: Array Processing

•We have discovered that our thermometers report temperatures that are 0.1 degrees below the actual value. Declare and initialize an array with the values 21.2, 22.0, 26.4, 24.5, 27.2 then program a loop to correct each value in place in the array. Print out the corrected values at the end of the program.

Page 14: Array Processing

•Several important Java facilities produce String arrays.

• Command line arguments.• String splitting.

•You can process String arrays in a manner similar to processing arrays of numbers.

Page 15: Array Processing

•Many programs that run from the command line allow you to provide extra arguments.

• The XP command, DIR, will display the directory contents but you can specify that these contents will display in wide format by specifying '/W'

dir /W

•The main() routine of all Java programs automatically gets all command arguments as a String array.

public static void main(String[] args)....

Page 16: Array Processing

public class DemoArguments { public static void main(String[] args) { for( String argument: args ){ System.out.println(“ “ + argument ); } }

Page 17: Array Processing

•JavaDirectory.html on the course website.

Page 18: Array Processing

•Lines are often entered from the console or from a file as a single string in a special format where special characters known as delimiters are used to separate items known as tokens.

Page 19: Array Processing

•Example: Excel can save data in a format known as CSV (Comma Separated Volume) where each column in each row is separated from other cells with a comma.

Malonga, John, 78,81, 78

Token

Delimiter

Page 20: Array Processing

import java.util.Scanner;public class DemoParseCSV{ public static void main(String[] args) { Scanner kb = new Scanner(System.in); String csvLine = kb.nextLine(); String[] tokens; tokens= csvLine.split(“,”); for( String token: tokens ){ System.out.println(“[“ + token.trim() + “] “ ); } }}

Page 21: Array Processing

•Write a program that takes a UPC code, a number of items as an int, and a price per item as a double and prints the total value of the order. You might have tto look up the use of Double.parseDouble(), and Integer.parseInt() and the use of NumberFormatException. Sample execution:Enter upc code, no. items, unit price separated by commas

v2111t,3,45.25Total Order Cost is 135.75