20 likes | 123 Views
This guide covers the basics of grouping multiple values of the same type using arrays in Java. It explains how to create, access, and manipulate integer arrays, including the nuances of referencing and copying arrays. Common pitfalls, such as misunderstanding array references, are highlighted to help you avoid mistakes. Additionally, learn how to read user input into arrays and utilize them in practical scenarios, such as implementing numerical methods like the trapezoid rule to compute areas under curves.
E N D
Array: How do you group multiple values (of the same type) together // Array optimizations int []ar = new int [] {2, 4, 6}; // equivalent to int []ar = new int[3];ar[0] = 2; ar[1] = 4; ar[2] = 6; // common mistakes. THIS DOES NOT // MAKE A COPY. ar and br point to // the exact same array structure int []br = ar; // the array which is jointly// pointed to by ar and br has now // changed. br[3] = 19; int [ ] ar; define a variable that refers to an array of int values After the statement above, what is the value of ar? ar null instantiate contiguous array of six possible int values ar = newint [6]; ar 0 1 2 3 4 5 ar [0] is the leftmost value ar [5] is the rightmost value An array can be traversed easily using a while statement. // INPUT Scanner sc = new Scanner (System.in); int []p = new int[3]; System.out.println ("Enter x, y, z values for point"); int i = 0; while (i < p.length) { p[i] = sc.nextInt(); i++; // ADVANCE } // array is now populated with p[0] .. p[2] CS2102 Handout: arrays
a b h = 1 Write program to read in a set of10 numbers, whose int values represent f(x) over the range 0..9Use the trapezoid rule to compute Where a=0 and b=9. For example,the area below is 56.5 Write From Scratch!!!!! CS2102 Handout: arrays