Chapter 2 - Arrays.Fall2009(1)

Chapter 2 - Arrays.Fall2009(1) - COP 3540 Data Structures...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 41 COP 3540 Data Structures with OOP Arrays Chapter 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 41 The Basics of Arrays in Java – Array Creation Two kinds of Arrays: primitive (ints, floats, double, char, unsigned, …) and objects In Java arrays are objects not primitives. Since all arrays are objects , must use ‘new’ to create. Two ways to create an array in Java 1 . int[ ] intArray; // only the reference intArray = new int[100]; // creates the array with space for 100 integers // intArray now points (references) this array. 2. Alternatively: int intArray[ ] = new int[100]; Either format is acceptable. (I prefer the latter) What is a primitive? Where is the array data actually stored? Where is the reference stored? Attribute : int arrayLength = intArray.length; // number of elements Where does this ‘length’ attribute come from? How do you know intArray.length is not a method ?
Background image of page 2
3 41 The Basics of Arrays in Java – Accessing Array Elements Array access is via an index number What is an index? Same as a subscript? temp = intArray[3]; //assign value of fourth element // in intArray to temp (assumes // temp is an int). intArray[7] = 66; // assigns value of 66 to // intArray[7]. (8 th element) Array Index Out of Bounds – discuss…. intArray = new int [100]; // bounds?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 41 The Basics of Arrays in Java – Initialization Unlike other languages, integer arrays are set to 0. autoData[ ] carArray = new autoData[4000]; “I have an array of autoData objects named carArray and have 4000 of them.” Each array element has null value (pointer value is null) No object values have been created. Assigning values to primitive array types: int[ ] intArray = {2,4,78,54,67}; // initialization list Also sets array to five elements: intArray[0] … inArray[4].
Background image of page 4
5 41 The Basics of Arrays in Java – Array Example – p1 // array.java - // demonstrates Java arrays This is an array of primitives / not objects!! class ArrayApp { public static void main(String[] args) { long[ ] arr; // reference to array // what is a ‘long?’ arr = new long[100]; // allocates array for 100 ‘longs.’ // ‘ long’ is a primitive or object? int nElems = 0; // number of items (to be used for size of arr) int j; // loop counter long searchKey; // key of item to search for //-------------------------------------------------------------- arr[0] = 77; // insert 10 items // brute force approach!! arr[1] = 99; // specifically assigning each element to arr[2] = 44; // a specific location in the array! arr[3] = 55; arr[4] = 22; // ’hard coded values” arr[5] = 88; arr[6] = 11; arr[7] = 00; arr[8] = 66; arr[9] = 33; nElems = 10; // now 10 items in array //-------------------------------------------------------------- for (j=0; j< nElems; j++) // display items // value of j upon leaving loop? System.out.print( arr[j] + " ");
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

Chapter 2 - Arrays.Fall2009(1) - COP 3540 Data Structures...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online