Single-Dimensional Arrays: Lecture Notes for Computer Programming 1, Exercises of Programming Languages

Lecture notes for computer programming 1 Faculty of Engineering and Information Technology

Typology: Exercises

2020/2021

Uploaded on 03/24/2021

mohammad-shubier
mohammad-shubier 🇵🇸

4.7

(3)

4 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 6
SINGLE-DIMENSIONAL ARRAYS
Lecture notes for computer programming 1
Lecture notes for computer programming 1
Faculty of Engineering and Information Technology
Faculty of Engineering and Information Technology
Prepared by:
Prepared by: Iyad
Iyad Albayouk
Albayouk
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Single-Dimensional Arrays: Lecture Notes for Computer Programming 1 and more Exercises Programming Languages in PDF only on Docsity!

Chapter 6

SINGLE-DIMENSIONAL ARRAYS

Lecture notes for computer programming 1Lecture notes for computer programming 1

Faculty of Engineering and Information Technology Faculty of Engineering and Information Technology

Prepared by: Prepared by:

IyadIyad

AlbayoukAlbayouk

What is an Array? What is an Array?

A single array variable can reference a largecollection of data.

Arrays have three important properties:

  • arrays represent a group of related data (for

example, temperature for the last five days, orstock prices for the last 30 days.)

  • all data within a single array must share the

same data type (for example, you can create anarray of ints or an array of floats, but youcannot mix and match ints with floats.)

  • The size of an array is fixed once it is created.

Using an array variable Using an array variable

Create an array variable called

studentGrades[10] studentGrades[10]

  • Declared as follows:

int int

studentGrades studentGrades

[] = new int[10]; [] = new int[10];

This sets up a location in memory for 10 integers whichcan be referenced using

studentGrades studentGrades

[ # ]

[ # ]

where

is the particular

student you want to look at.

Array Naming Considerations Array Naming Considerations

The rules for naming variables apply whenselecting array variable names

  • Composed of letters, digits, dollar signs and

underscore characters

  • Cannot start with a digit

Follow all the good programming hintsrecommended for variable names

  • i.e. just think of it as an ordinary variable when

making up the name

  • In addition it is bad style to end an array name

with a digit

  • ie.

array3[5] array3[5]

Elements Elements

Refers to the individual items represented by thearray. For example,

  • an array of 10 integers is said to have 10

elements

  • an array of 15 floats has 15 elements– an array of 5 characters has 5 elements– and so on…

Index (Position Number or Subscript) Index (Position Number or Subscript)

Refers to one particular element in the array

Also known as position number or, more formally,as a

subscript

The first element in an array is represented by anindex or subscript of 0 (zero). For example,

studentGrades studentGrades

[ 0 ]

[ 0 ]

This first position is known as the

zeroth element

The second position is referred to by

studentGrades studentGrades

[ 1 ]

[ 1 ]

Array positions (cont Array positions (cont

d) d)

We can access them by specifying the array namefollowed by square brackets with the indexnumber between them. For example,

System.out.println System.out.println

("The third student ("The third student

’’

s grade is " s grade is "

+ +

studentGrades studentGrades

[ 2 ] ); [ 2 ] );

Would print only the third integer spot in the array(remember 0 is the first, 1 is the second, and 2 isthe third element’s index / subscript)

The output would look like the following:

The third student’s grade is 99

Array positions (cont Array positions (cont

d) d)

The index scheme starting at 0 may be initiallyconfusing.

This is the cause of many off-by-one errors sostudy the concept carefully

The element in the array’s first position issometimes referred to as the zeroth element.

Notice the difference between "position" and"element number"

Keyword new Keyword new

As we will see, the keyword new is used in Javawhen you wish to create a new object.

In Java, arrays are objects.

As with all data members of objects, the elementsof the each position in a new array willautomatically be initialized to the default value forthe array’s type.

Some powerful features of arrays Some powerful features of arrays

Can use expressions as the subscript

E.g.

if variables a = 1 and b = 2 if variables a = 1 and b = 2

studentGrades studentGrades

[ a + b ] would be the same as writing [ a + b ] would be the same as writing

studentGrades studentGrades

[ 3 ] [ 3 ]

Can use array elements in expressions

E.g.

int int

gradeTotal gradeTotal

= 0 ; = 0 ;

gradeTotal gradeTotal

= =

studentGrades studentGrades

[ 0 ] +[ 0 ] +

studentGrades studentGrades

[ 1 ] + [ 1 ] +

studentGrades studentGrades

[ 2 ] + [ 2 ] +

… …

etc etc

… …

studentGrades studentGrades

[ 9 ] ; [ 9 ] ;

Would add up all the array elements and store them in gradeTotal.

So how do we use arrays? So how do we use arrays?

Same concepts as other variables apply

  • Must declare the array– Must initialize the array (unlike regular

variables, Java will automatically initializearrays with default values)

  • Arrays variables are actually reference variables.
    • Can use arrays in expressions and methods,

setting elements’ values or using their values,similar to the use of ordinary variables

Declaring an array Declaring an array

First you can declare an array reference variable (you mustspecify the

type

of the elements in the array) :

intint

[][]

myFirstArraymyFirstArray

;;

//declares an array //declares an array

//variable for //variable for

ints ints

Note: the line above does not allocate memory for the array (itcannot since we have not said the size of the array yet). It only setsaside enough memory to reference the array. Until we create anarray, the reference is to

null

.

Next, we create the array and “point” the reference to it:

myFirstArraymyFirstArray

= new= new

intint

[10];[10];

To create the array, we need the

number

of elements in the array so

the computer can set aside adequate memory for the array.

We can combine the declaration and allocation lines intoone line as follows:

intint

[][]

myFirstArraymyFirstArray

= new= new

intint

[10];[10];

Declaring arrays (cont) Declaring arrays (cont)

You can use a constant to set the size of the array

final final

int int

NUM_STUDENTS_IN_CLASS = 8 NUM_STUDENTS_IN_CLASS = 8

int int

[] exams = new [] exams = new

int int

[NUM_STUDENTS_IN_CLASS ]; [NUM_STUDENTS_IN_CLASS ];

In Java, you do not need to know the size of the array atCOMPILE time. Instead you can know the size at RUNtime. For example, the following is legal:

inputString inputString

= =

JOptionPane.showInputDialog JOptionPane.showInputDialog

("How ("How

many students ?");many students ?");

int int

students = students =

Integer.parseInt Integer.parseInt

( (

inputString inputString

); );

int int

[] []

myFirstArray myFirstArray

=

new

=

new

int int

[students]; [students];

Initializing an Array Initializing an Array

You can initialize an array when you declare it, asyou do with other variables

Syntax is slightly different, as you are nowinitializing more than one element at a time

One way at declaration, using

initializers

int int

myFirstArray myFirstArray

[

] = { 0, 0, 0, 0, 0 };

[

] = { 0, 0, 0, 0, 0 };

Note the braces around the initial zeroes whichthemselves are separated by commas.

Also note that creating arrays in this way eliminatesthe need for the keyword new.