SINGLE-DIMENSION ARRAYS
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
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
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];
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]; }
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
What Usually Happens to an Array Insertion – At the beginning – At the end – Somewhere in the middle Deleting Traversing Sorting Searching
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!