Balances issingle gpas note the above declarations do

This preview shows page 2 - 3 out of 5 pages.

balances isSingle gPAs . . . Note: the above declarations do not set aside any space for the elements of the arrays After the declarations, these variables store the value null (which means they don't point to anything). More about null later in the course. 8 Creating the array space The keyword, new, is used to create the array space for all the elements of the array. int [] balances; boolean [] isSingle; double [] gPAs; balances = new int [ 79 ] ; isSingle = new boolean [ 5 ] ; gPAs = new double [ 6 ] ; When we create the array space we need to decide the number of elements in the array. (This can not be changed later.) balances isSingle gPAs a a b b c c 9 Default value of array elements When we create an array, all the elements are initialised automatically to standard default values: balances isSingle gPAs a a b b c c 10 Assigning values to array elements Once the array space has been created, we can assign values to each element of the array. The index value identifies which element we are assigning to. int[] balances = new int[79]; boolean[] = new boolean[5]; balances [3] = 998765; isSingle [3] = true; balances [1] = 5; isSingle [4] = isSingle [3] ; balances [78] = balances [1] + balances [3] ; balances isSingle a c a c Assigning values to array elements We can initialise arrays as shown on the previous slide or we can declare and initialise in one statement. Note: this can only be done at declaration time. int[] numbers = {3, 8, 4, 9, 13, 5, 23, 14 }; a This statement creates the array space and initialises each element. int[] numbers = new int[8]; numbers[0] = 3; numbers[1] = 8; numbers[7] = 14; numbers a 11 12 The length field of an array Every array has a length field which stores the number of elements in the array. The length field can be accessed using dot notation: balances isSingle gPAs a a b b c c int[] balances = new int[79];
Image of page 2

Subscribe to view the full document.

Image of page 3
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern