lecture-30c - Object Oriented Programming Java - Arrays...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Object Oriented Programming Java - Arrays Array Basics In Java, an array is an indexed collection of data values of the same type. Arrays are useful for sorting and manipulating a collection of values. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 2 Array Basics Square brackets are used to declare an array. To declare an array, we may attach the brackets to either the data type or the variable name. An array named rainfall of type double, may be stated either as double rainfall; or double rainfall; Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 3 Array Basics In Java, an array is a reference data type. We use the new operator to allocate the memory to store the values in an array. rainfall = new double [12]; //creates an array of size 12. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 4 An array of 12 double values. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 5 Array Basics We use a single identifier to refer to the whole collection in the array. We use an indexed expression to refer to the individual values of the collection. Arrays use zero-based indexing. An individual value in an array is called an array element. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 6 An array of 12 double values after all 12 are assigned values. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 7 Array Basics An array has a public constant length for the size of an array. for (i=0; i<rainfall.length; i++){ ... } This approach is particularly useful when the size of an array may change, or is not known in advance. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 8 Array Basics Do not confuse the length value of an array and the length method of a String object. The length is a method for a String object, so we use the syntax for calling a method. String str = “This is a string”; int size = str.length(); Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 9 Array Basics We can initialize the array at the time it is declared: String monthName = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; Because the array is initialized here, it is unnecessary to state the size of the array. The size is determined by the number of values in the list. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 10 Array Basics Using constants to declare the array sizes does not always lead to efficient use of space. Declaration of arrays with constants is called fixed-size array declaration. Fixed-size array declaration may pose two problems: Not enough capacity for the task at hand. Wasted space. Object Oriented Programming Java - Arrays R.B. Lenin Winter 2006 DA-IICT 11 ...
View Full Document

This note was uploaded on 09/15/2010 for the course CSE IT115 taught by Professor Rblenin during the Spring '10 term at Punjab Engineering College.

Ask a homework question - tutors are online