# Week7 - Chapter 7 Introducing Arrays As weve learned using...

This preview shows pages 1–3. Sign up to view the full content.

Chapter 7 Introducing Arrays As we’ve learned, using variables makes your programs flexible. Thanks to variables, we can conveniently store data in our programs and retrieve the data later by name. Until now, you’ve learned about various types of numerical variables, including integer, long integers, floating- point, and double floating-point variables. Now --- a handy data structure called an array. Often in your programs, you’ll want to store many values that are related in some way. Suppose you are writing a program that manages student grades. Assume a class has 10 students. One method of storing this information would be to declare 10 variables and assign scores into each separate variable. int score1=70, score2=80, score3=75, score4=60, score5=95, score6=85, score7=90, score8=65, score9=55, score10=90; A subsequent reference to a score is cumbersome. For example, the code to display each score requires 10 separate System.out.println statements such as the following: System.out.println(“Student #1’s grade =” + score1); System.out.println(“Student#2’s grade =” + score2); System.out.println(“Student#3’s grade =” + score3); System.out.println(“Student #10’s grade =” + score10);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A one-dimensional array can enable an efficient storage and retrieval of this kind of information. A one-dimensional array is a string of contiguous storage locations located in primary memory. To create an array, you first must create an array variable of the desired type. The general form of a one-dimensional array declaration is: type var-name[ ]; The type determines the data type of each element that comprises the array. The statement: int score[ ]; defines a variable name that can point to a contiguous string of integers in primary memory. Initially, score is set to null , which represents an array with no value. To link score with a physical array of integers, you must allocate one using new and assign it to score. The new operator allocates memory.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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