1 / 21

Array Processing

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.

hans
Télécharger la présentation

Array Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Processing

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

  3. 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.

  4. 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); }

  5. 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); }

  6. 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.

  7. 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); } }

  8. //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]);; } } }

  9. 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.

  10. 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]; }

  11. //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]); } } }

  12. Your Turn!

  13. 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.

  14. 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.

  15. 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)....

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

  17. JavaDirectory.html on the course website.

  18. 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.

  19. 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. Token Malonga, John, 78,81, 78 Delimiter

  20. 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()‏ + “] “ ); } } }

  21. 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.25 Total Order Cost is 135.75

More Related