single-dimension arrays. data structures it is a collection of scalar data types that are all...
TRANSCRIPT
![Page 1: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/1.jpg)
SINGLE-DIMENSIONARRAYS
![Page 2: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/2.jpg)
Data Structures
• It is a collection of scalar data types that are all referenced or accessed through one identifier– scalar type – it is a simple data type that holds one
value at a time. Ex. int, double, char, and boolean
•An array is a data structure
![Page 3: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/3.jpg)
Arrays
• It is a linear data structure• it is composed of adjacent memory locations (or
cells)• They are objects in Java• You must declare the reference and instantiate it
using new• It will hold default values:– 0 for numeric– false for Boolean– null for objects
![Page 4: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/4.jpg)
Declaring an array
type[] arrayName = new type[length];
An array object is constructed of a defined size. Once it has been constructed the number of slots does not change
Generally, the size of an array is defined by using a final value:final int MAX = 200;int[] numb = new int [MAX];
![Page 5: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/5.jpg)
Initializing an Array
• Arrays are initialized in some type of loop.• To make a copy of an array, you must retrieve
each value of the initial array and make a copy of it’s value to store into the copy array.
• for(int x = 0; x <= MAX; x++){
arrayCopy[x} = arrayOriginal[x];}
![Page 6: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/6.jpg)
Common Mistakes about Arrays
• copyArray = originalArray
• = Is an assignment operator, all you did was assign the memory location of the originalArray is assigned into copyArray memory cell
originalArray
copyArray
![Page 7: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/7.jpg)
What Usually Happens to an Array
• Insertion – At the beginning– At the end– Somewhere in the middle
• Deleting• Traversing• Sorting• Searching
![Page 8: SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type](https://reader036.vdocuments.mx/reader036/viewer/2022082505/56649ee65503460f94bf6192/html5/thumbnails/8.jpg)
Arrays as Parameters
• When a method is sending an array, the method’s formal parameter becomes an alias for the original calling parameter.
• Let’s see a program!