Arrays – Part 4

Part 4 – 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 43 – 63)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. Two Dimensional Arrays
  2. Traversing Two Dimensional Arrays
  3. Using Objects for GUI Purposes

Two Dimensional Arrays

  • A two-dimensional array is essentially an “array of arrays
  • A multidimensional array can be seen as a table of rows and columns

Captusdgsre(33)

  • This is also known as a matrix
  • It can also be thought of as a single array, in which every element in the array is another array
  • A matrix can be declared with the new operator to allocate memory to the matrix or by initializing the matrix to preset values (which also allocates memory when it does this)
  • For example, a 3D array could be visualized as…

Captufhdre1(6)

  • You declare a matrix the same way you declare another array by:
    • Putting the type
    • Then the name
    • Then the sets of [] (or put the sets of [] after the type)
    • Then the sign
    • Then the new operator
    • Then the type again
    • Then the [] with the dimension size inside each set
  • For example…

Picturgsde12(1)

  • There is no limit to the number of dimensions you wish to use
  • You can also create an initialized matrix by using nested sets of the {} braces and commas
  • For example…

Picture1dsg3

  • There is no limit to the number of dimensions you wish to use
  • You can create 3 dimensional arrays (an array of arrays of arrays), 4 dimensional arrays (an array of arrays of arrays, of arrays), 5 dimensional arrays (an array of arrays of arrays…), etc.
  • Although some of these arrays cannot be visualized in our geometry system (which only has 3 dimensions)

Traversing Two Dimensional Arrays

  • Nested For loops are used to traverse across matrices and access the elements in the rows and columns, let’s try this example:

Picture1dgdsg(1)

Picturefhdh15

Picture1dssd6

  • Notice how the first loop goes from 0 (always the starting value of an array) to array.length which is the number of ‘rows’ in a 2D arrayCapturegsd2(21)
  • However, the second loop goes from 0 to array[0].length which is the length of the first element of the 2D array (which is another array)

Capture3sdgsg(13)

  • To actually assign to a matrix, you must give both dimensions in assignment

Capturdie4(12)


Using Objects for GUI Purposes

  • The study of objects will be upcoming in future units, but in order to make assignments more interesting, we will examine some uses of Java objects today
  • An object is defined as:
    • A finite collection of properties (variables) representing the properties of a specific physical or conceptual object
    • A finite collection of designated functions called methods for observing and changing the properties of that object
  • Today we will examine a few graphical objects for GUI purposes in Labs 3 and 4 that are part of Java’s core libraries (java.) and extension libraries (javax.)
  • In later units we will be making our own objects, but today we will examine some imported ones
  • First I will give you some sample code to try, then explain it after…

Capturffdhe5(7)

  • Notice how we import the entire swing library by using the * to mean everything (it is a wildcard character)

Pictfdhdure17

  • We also import the awt library which stands for abstract windows toolkit (a set of ‘windows’ like tools)

Picturdsgsge18

  • This line creates a object from the swing library called a JTextArea object (J for Java) that has a lot of nice features for displaying text
  • Notice how we must use the operator new because it is an object and we have to allocate memory for it
  • Now we apply the same principals to creating 2 more objects, a Color object, and a Font object
  • Both of these objects come from the awt library

Picturdsge19

  • This line creates a object from the awt library called a Color object that has a lot of nice features for using colors
  • As well, when it is declared (constructed) and the operator new is used notice how there is numbers places inside of the round braces () which are actually used to set the amount of red, green, and blue to be used (a value between 0-255)

Picture2fdhdh0

  • This line creates a object from the awt library called a Font object that has a lot of nice features for using fonts, when constructed, it sets the font, the style of the font, and the size of the font
  • These objects (JTextAreaColor, and Font objects) can now be used

Picturegsdg21

  • These lines of code are built-in methods (functions) of the object (classJTextArea
  • They are used to change some properties of the object
  • Specifically they set:
    • Set a background color (.setBackground(Color colorObject))
    • Set the font (.setFont(Font fontObject))
    • Set the text to display (.setText(String textToDisplay))
  • For the JTextArea object
  • Another example…

Picturesfdghs22

  • This line creates a object from the swing library called an Icon object that has a lot of nice features for using graphics
  • When declaring it, you use the ImageIcon(“string”) constructor to assign a specific graphic (jpg or gif) to the Icon and the entire path must be included to work in NetBeans

Picturefdhfd23


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