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

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

View Full Document Right Arrow Icon
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);
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

Page1 / 11

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online