CIS304 Chp 10-13

CIS304 Chp 10-13 - CIS 304 Arrays / Exceptions CIS 304...

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

View Full Document Right Arrow Icon
CIS 304 Arrays / Exceptions CIS 304 Intermediate Java  Programming For Business
Background image of page 1

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

View Full DocumentRight Arrow Icon
How to create an array Arrays can store more than one primitive type or object. An  element is one of the items in an array Syntax for Creating an array Datatype[] arrayVariable = new Datatype[size]; Syntax for assigning values to an array arrayVariable[index] = value; Create arrays then assign values: double[] prices = new double[2];  prices[0] = 12.95; prices[1] = 24.95; String[] names = new String[2]; names[0] = “Homer Simpson”; names[1] = “Bart Simpson”; Create and assign values at the same time: double[] prices = {12.95, 24.95}; String[] names = {“Homer Simpson”,”Bart Simpson”}
Background image of page 2
Using loops with arrays Since arrays are object they have properties.  arrayVariable.length  is a property that returns the size of  the array (how many elements it contains) Syntax of using a for loop with an array. Double prices = {12.9,13.3,14.5}; For (int I = 0; I < prices.length;i++){ System.out.println(“The price at index “ + i + “ is “ + prices[i]); } Using the enhanced for loop (available in JDK v1.5 and above) Double prices = {12.9,13.3,14.5}; For (double aPrice : prices){ System.out.println(“The price at index “ + i + “ is “ + aPrice); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
Using the Array Class Java has a built in Array class that provides  static methods to perform operations on arrays. Array Class
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

CIS304 Chp 10-13 - CIS 304 Arrays / Exceptions CIS 304...

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

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