lecture08

lecture08 - 1.00/1.001 - Lecture 8 Arrays and ArrayLists...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1.00/1.001 - Lecture 8 Arrays and ArrayLists Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type Built-in types (int, double, etc.) or Objects (Students, Dates, etc.) Arrays are part of the Java language Arrays are objects, not primitives like int or double . They are declared in the same way as other objects int intArray= new int[20]; The array object has an int data member, length, that gives the number of elements in the array: int aSize= intArray.length; // aSize= 20 Each value is accessed through an index intArray[0]= 4; intArray[1]= 77; 1 Arrays, p.2 Array index always starts at 0, not 1 An array with N slots has indices 0 through N-1 intArray has elements intArray[0] through intArray[19] Array lengths cannot be changed once they are declared Arrays can be initialized when declared int intArray= {5, 77, 4, 9, 28, 0, -9}; // Note that new is implicit (not needed) in this case Arrays of numerical values are zero when constructed Copying Arrays To copy an array, use arraycopy( ) method of System class, as in the following (assuming intArray exists) int newArray= new int[intArray.length] // arraycopy(fromArray, fromIndex, toArray, toIndex, count) System.arraycopy(intArray, 0, newArray, 0, intArray.length); // Now intArray and newArray have separate copies of data Note: Arrays dont have to be same length as long as segment copied fits into destination array 2 Copying an array reference If we had just defined newArray without copying: int newArray= intArray; newArray[2]= -44; // This sets // intArray[2]= -44 also // intArray and newArray would // be two names for the // same array. Remember in Java // references refer to objects Copying reference to array int[ ] 5 77 4 9 28 0 9 intArray newArray intArray= {5, 77, 4, 9, 28, 0, 9}; int[ ] newArray = intArray; 3 Copying entire array int[ ] int[ ] 5 77 4 9 28 0 9 5 77 4 9 28 0 9 intArray= {5, 77, 4, 9, 28, 0, 9}; newArray = new int[intArray.length]; System.arraycopy(intArray,0,newArray,0,intArray.length) intArray newArray Looping Over Array Java 1.5 introduced new way of looping over a collection. If arrDouble is a reference to array of doubles, the old way is: double sum = 0.0; for(int i=0; i<arrDouble.length; i++) sum = sum + arrDouble[i]; 4 Looping Over Array Java 1.5 In Java 1.5, we can loop over elements of a collection as follows : double sum = 0.0; for(double d : arrDouble) sum = sum + d; Multidimensional Arrays You can create 2, 3, ..n dimensional arrays in Java. int[ ][ ] twoDArray = new int[5][10]; A 2D array is actually an array of 1 D arrays, so that twoDArray[3][5] Refers to the sixth element of the fourth array of integers....
View Full Document

Page1 / 19

lecture08 - 1.00/1.001 - Lecture 8 Arrays and ArrayLists...

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