Array Processing

Download Report

Transcript 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.
Linear searching of an array.
Technique involves looping while iterating a
variable to take on values between 0 and n-1.
•
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);
}
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);
}
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.
•
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);
}
}
//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]);;
}
}
}
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.
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];
}
//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]);
}
}
}
Your Turn!
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.
•
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.
•
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)....
public class DemoArguments {
public static void main(String[] args) {
for( String argument: args ){
System.out.println(“ “ + argument );
}
}
JavaDirectory.html on the course website.
•
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.
•
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
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()
+ “] “ );
}
}
}
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