Creating Arrays in Java

Simple data types of the kind we saw in the previous section are fine for storing single data items, but data is often more complex. Like JavaScript, Java supports arrays as well. Here's an example. In this case, I'll store the balances in customers' charge accounts in an array named chargesDue . I start by declaring that array, making it of type double :

 public class ch10_04  {     public static void main(String[] args)     {  double chargesDue[];  .     .     . 

Besides declaring the array, you have to allocate the number of elements you want the array to hold. You do that using the new operator:

 public class ch10_04  {     public static void main(String[] args)     {         double chargesDue[];  chargesDue = new double[100];  .     .     . 

You can combine the array declaration and definition into one statement like this:

 public class ch10_04  {     public static void main(String[] args)     {  double chargesDue[] = new double[100];  .     .     . 

After the array has been created, you can address individual elements using square brackets and an array index, like this:

Listing ch10_04.java
 public class ch10_04 {     public static void main(String[] args)     {         double chargesDue[] = new double[100];  chargesDue[4] = 99.06;   System.out.println("Customer 4 owes $" + chargesDue[4]);  } } 

Here's the results of this code:

 %java ch10_04  Customer 4 owes .06 

Java Arrays

In Java, the lower bound of an array that you declare this way is 0, so the statement chargesDue = new double[100] creates an array with a first item of chargesDue[0] and a last item of chargesDue[99] .

You can also initialize arrays with values at the same time you create them. You do that by specifying a comma-separated list of values in curly braces like this (note that the number of elements in the created array will be the number of elements in the list):

 public class ch10_04  {     public static void main(String[] args)     {         double chargesDue[] = {1093.66, 667.19, 45.99, 890.30, 99.06};         System.out.println("Customer 4 owes $" + chargesDue[4]);     } } 

I'll elaborate this example now. Say that the store we're handling customer balances for opens a new branch, so now there are both eastern and western branches. If customers can open accounts in both branches, we'll need to keep track of two balances for each customer. You can do that by using a two-dimensional array like this:

 public class ch10_05  {     public static void main(String[] args)     {  double chargesDue[][] = new double[2][100];  .     .     . 

Now you refer to every element in the array with two array indices, not just one, as in the previous one-dimensional version of this array:

 public class ch10_05  {     public static void main(String[] args)     {         double chargesDue[][] = new double[2][100];  chargesDue[0][4] = 99.06;   chargesDue[1][4] = 23.17;  .     .     . 

I can display the balance in both a customer's eastern and western branch accounts like this:

Listing ch10_05.java
 public class ch10_05 {     public static void main(String[] args)     {         double chargesDue[][] = new double[2][100];  chargesDue[0][4] = 99.06;   chargesDue[1][4] = 23.17;   System.out.println("Customer 4 owes $" + chargesDue[0][4] + " in the eastern graphics/ccc.gif branch.");   System.out.println("Customer 4 owes $" + chargesDue[1][4] + " in the Western graphics/ccc.gif branch.");  } } 

Here's the result:

 %java ch10_05  Customer 4 owes .06 in the eastern branch. Customer 4 owes .17 in the western branch. 

You can also initialize a two-dimensional array by assigning values when declaring such an array:

 public class ch10_05  {     public static void main(String[] args)     {  double chargesDue[][] = {{1093.66, 667.19, 45.99, 890.30, 99.06},   {2019.00, 129.99, 19.01, 630.90, 23.17}};  System.out.println("Customer 4 owes $" + chargesDue[0][4] + " in the Eastern graphics/ccc.gif branch.");         System.out.println("Customer 4 owes $" + chargesDue[1][4] + " in the Western graphics/ccc.gif branch.");     } } 

Find the Length of an Array

Need to find the length of an array? Just use the array's length property, like this: scores.length .



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net