Arrays – Part 1

Part 1 – Unit 3: Arrays (Computer Science 30S)

If you don’t feel like reading this web page, here are two alternatives:

  • Read this presentation of the same content (slides 1 – 17)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. What are arrays?
  2. What is An Array in Computer Science?
  3. Declaring (allocating) Arrays

What Are Arrays?

  • Up to this point in your Computer Science education, you have been manipulating data by using simple variables to store the data
  • Variable data type are known as simple, atomic, or primitive data types
  • Now we will examine the first complex or advanced data type, known as an array
  • Often we work with data that is part of a group with some overall structure
  • For instance, we could be working with a page of text rather than the individual characters, or working with a book, rather than the individual pages
  • Also, consider the example of a deck of cards…
  • If you take a card, you must decide how to represent a card in a program
  • You could create 52 string variables, named card1card2, etc then load each variable with a description such as card1 = “Ace of Clubs”; card2 = “Two of Diamonds”; etc.
  • However the problem with this approach is that you will have to deal with 52 different variables, and there is no good way to manipulate all the variables as a whole for operations such as shuffling and dealing
  • A better approach is to group all the data together in a structured way, in an array called deck that has 52 entries ranging from 1 to 52

Capturedd(25)

  • Now you only have one variable to worry about (with 52 entries)
  • You can easily access and manipulate any element of the array by referring to its element number (or subscript) but can still manipulate the deck as a whole as well

Captured1(2)


What is An Array in Computer Science?

  • In Computer Science, an array is defined as a group of memory locations (variables) stored continuously in memory
  • Each have the same name, and are all of the same type
  • Each variable in the array is called a member, or element and is identified by a number called an index
  • For example, an array of integers called array can be visualized as:

Capture3gg(11)

  • In Java (and most programming languages), indexing begins with 0
  • The numbers inside the parenthesis after the array name indicate which element you are referring to
  • The number inside parenthesis is called the arrays subscript

Capturefff4(10)


Declaring (allocating) Arrays

  • Java arrays are objects that take up memory (just like regular variables do)
  • However, unlike regular variables, arrays as objects need to be allocated memory dynamically using the operator new
  • When we declare the array, we specify the array typearray name, and array size (which is optional at the time of declaration)
  • Optionally as well, we can also initialize all array elements at the time it is declared
  • For example…

Capture(ggg26)

  • An array of type double called array is declared, the array is then allocated 5 slots of memory (it is dimensioned 5 slots)
  • We assign a 301 to array subscript 3
  • Notice that we use squared brackets [] in Java NOT round brackets () as these are used for functions
  • Also the brackets are put on the type in the declaration (they can also be put on the name)

Capture2(1hh8)

  • All arrays in Java begin their indexing at 0
  • Array subscripts are of an unknown value when initialized
  • Another option is to just declare an array with the name and type, and later allocate the memory to it

Picturedd1(16)

  • However, if you try to assign to a subscript before you allocate memory to the array, an error occurs, for example:

Picture2(6ff)

  • Generates this error:

Picture3(dd5)


The NEXT thing to work on is the “Arrays Example 1” example page