computer science, programming and critical thinking, Slides of Computer Science

it contains critical thinking slice and notes, programming and computer science..

Typology: Slides

2021/2022

Uploaded on 12/28/2022

HackersHood
HackersHood 🇬🇭

5

(1)

3 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download computer science, programming and critical thinking and more Slides Computer Science in PDF only on Docsity!

Arrays

Arrays

  • (^) Review of java data types
  • (^) As we discussed in the previous lessons, data types is a set of

data values, along with a set of operations on those values.

  • (^) Integer, real, character and Boolean data types are said to be

atomic because they have no component parts that can be

accessed separately.

  • (^) For example a single character is atomic, but the string

“Good Morning” is not atomic because it is composed of 12

characters.

  • (^) In a composite data type, each value is a collection of

component items.

  • (^) The entire collection is given a single name, yet each component

can still be accessed individually.

  • (^) The class is an example of composite data type.
  • (^) A class has a name and can be composed of named data fields

and methods.

  • (^) The data fields and methods can be accessed individually by

name.

  • (^) A class is unstructured because access does not depend on an

ordering relationship among the data fields or the methods.

  • (^) That is, if we change the order in which the members of the class

are listed, this change has no effect on how we access them.

  • (^) For example, here is the same class, declared with its data fields in two different order: class Example { int field1; int field2; double field3; } class Example { double field3; int field2; int field1; }

What is an array

  • (^) Arrays are data structures consisting of related data items of the same type.
  • (^) Thus, an array is a group of variables or containers that is given only one name.
  • (^) The group of variables are placed under a single umbrella with only one name.
  • Arrays make it convenient to process related groups of values or values of the same type.
  • (^) Arrays remain the same length once they are created.
  • (^) Each variable in the group that makes up an array is called elements or components.
  • (^) An array element is given a number to make references to it instead of a name.
  • (^) An array is a variable that contains other variables of values that are all of the same type.
  • (^) Arrays are objects, hence, they are considered as reference data types.
  • (^) However, the elements of an array can be of either a primitive type or reference type.

Declaring an Array

  • (^) Syntax for one dimensional array declaration
    • (^) Data-Type[] Array-Name;
    • Data-Type describes what is stored in each component of the array
    • (^) The brackets following Data-Type indicate that this structure is an array of Data-Type elements.
    • (^) ArrayName is a location in memory that will hold the address of an array when that array is instantiated.
    • (^) For example,
      • (^) int[] numbers;

Creating/instantiating an

array

  • (^) You create an array just like you create an object; you use new.
  • Following is the syntax template for instantiating an array.
  • (^) Notice that arrays don’t need to be initialized, so we don’t pass a list of arguments.
  • (^) Instead, we put the number of slots to be in the array in brackets beside the type of the array.
  • (^) Syntax for creating one dimensional array
  • (^) Array-Name = new Data-Type[Int-Expression];
  • (^) The angle array has four components, each capable of holding one float value.
  • (^) The testScore array has a total of ten components, all of type int.
  • (^) Java provides an alternative syntax for declaring an array variable.
  • (^) You can place the brackets that signal an array after the array name, as shown here:
  • (^) char letters[];
  • (^) char upperCase[];
  • (^) char lowerCase[];
  • (^) Look closely at this example: letters and upperCase are composite variables of type char[], but lowercase is a simple variable of type char.
  • (^) If you use the syntax that we introduced first, you cannot forget to put the brackets on one of the array identifiers:
  • (^) char[] letters, upperCase, lowerCase;
  • (^) Java also provides an alternative way to instantiate an array.
  • (^) You learned previously that Java allows you to initialize a variable in its declaration:
  • (^) int delta = 25;
  • (^) The value 25 is called an initializer.
  • (^) You can also initialize an array in its declaration, using a special syntax for the initializer.
  • (^) In this case,you specify a list of initial values for the array elements, separate them with commas, and enclose the list within braces:
  • (^) int[] age = {23, 10, 16, 37, 12};

Accessing Individual

Components

  • (^) To access an individual array component, we write the array name, followed by an expression enclosed in square brackets.
  • (^) The expression specifies which component to access.
  • (^) The syntax template for accessing an array component follows:
  • (^) Array-Name[Index-Expression]