no9.e10a.2017.pdf - Arrays of Primitive Types double results = new double[5 results[2 = 3.4 results[4 =-0.5 index value[0 0.0[1 0.0[2 3.4[3 0.0[4-0.5

# no9.e10a.2017.pdf - Arrays of Primitive Types double...

• 6

This preview shows page 1 - 3 out of 6 pages.

121 Arrays of Primitive Types double [] results = new double[5]; results[2] = 3.4; results[4] = -0.5; index [0] [1] [2] [3] [4] value 0.0 0.0 3.4 0.0 -0.5 index [0] [1] [2] [3] [4] [5] value false false false true false false boolean [] tests = new boolean[6]; tests [3] = true; 122 Array Initialization & Traversal Arrays may be explicitly initialized when created. For example, String [] houses = {“Pforz”,“Quincy”,“Dunster”,“Foobar”}; Array variables are reference variables; i.e., an array variable contains a pointer, just as ordinary objects do. Every array object contains a public field named length. What will the following code print? for (int i = 0; i < houses.length; i++) System.out.println (houses[i].length() -1 ); 123 "Array Mystery" Problem traversal : An examination of each element of an array. What values are stored in the following array? int [] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } }
124 Limitations of Arrays You cannot resize an existing array: int [] a = new int[4]; a.length = 10; // error You cannot compare arrays with == int [] a1 = {42, -7, 1, 15}; int [] a2 = {42, -7, 1, 15}; if (a1 == a2) { ... } // false! An array cannot print itself: int[] a1 = {42, -7, 1, 15}; System.out.println(a1); // [[email protected]] 125 Practice with Simple Arrays Create an array to keep track of the scores in college basketball games by different teams

#### You've reached the end of your free preview.

Want to read all 6 pages?

• Fall '17
• HenryH.Leitner

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern