Home arrow Java arrow Page 2 - Making Decisions, Decisions
JAVA

Making Decisions, Decisions


Most JavaServer Pages will need to perform one of several actions according to some condition. This is managed in the code through control statements, which come in three flavors: conditional, iterative, and branching. This article will explain the syntax Java provides for these statements, how and when to use arrays, and more. It is taken from chapter five of Beginning JSP 2 From Novice to Professional, written by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 05, 2005
TABLE OF CONTENTS:
  1. · Making Decisions, Decisions
  2. · Creating Multidimensional Arrays
  3. · Comparing Data Values
  4. · Making Decisions
  5. · Understanding Variable Scope in Statement Blocks
  6. · How It Works
  7. · Understanding the Conditional Operator
  8. · Trying It Out: Working with the choose...when...when Construct
  9. · Understanding Loops and Iteration
  10. · Introducing Branching Statements
  11. · Trying It Out: Using Arrays

print this article
SEARCH DEVARTICLES

Making Decisions, Decisions - Creating Multidimensional Arrays
(Page 2 of 11 )

So far, the arrays you’ve seen have had a single index value to access elements they contain. You can visualize them as simple lists of data items, where the index specifies the number of the required item in the list. Such arrays are known as one-dimensional because just one index can uniquely identify any one particular member (see Figure 5-1).


Figure 5-1.
  A one-dimensional array 

However, arrays can have more than one dimension. For instance, a two-dimensional array has two indices. You can visualize such arrays as a set of pigeonholes, and to locate one specific pigeonhole, you need to know both how far from the left side it is, and how far down from the top it is (see Figure 5-2).


Figure 5-2.  A two-dimensional array 

For instance, say you had two houses in your street, and each house had three occupants. You could store the names of the occupants in a two-dimensional String array, where the first index would indicate the house in question and the second one would indicate a particular occupant.

Creating a two-dimensional array is similar to creating an ordinary one-dimensional array:

String[][] houses = {
  { "Joey", "Tommy", "Podlington" },
  { "Pickles", "Scragg", "Floopy" },
};

Note that you need two empty brackets after the data type and that you initialize using a set of values each contained in curly braces. In Java, the general syntax for creating two-dimensional arrays of size  m x n (where m and n are positive integers) is as follows:

DataType[][] array_variable_name = {
  {value1, value2, ..., valuen},
  {value1, value2, ..., valuen},
  .
  .
  .
  {value1, value2, ..., valuen}, //mth array of values
}

Referencing elements in a two-dimensional array is equally straightforward. For instance, you could find the name of the second member of the first house using houses[0][1] (don’t forget that array indices always start at zero!). Also, you could indicate the whole of the first house using houses[0]. In other words, if you want to reference all occupants of the mth house, you’d use houses[m-1], and if you want to reference the nth occupant of the mth house, you’d use both indices: houses[m-1][n-1].

NOTE  Another way to think of two-dimensional arrays is as an array of arrays—that is, as a one-dimensional array where each element is itself a one-dimensional array. Thus, when you access the array, the first index specifies which “subarray” to use, and the second index specifies the offset within that subarray.

One thing to be aware of is that Java allows so-called jagged arrays. This is when the arrays within an array are of different length. For instance, if the two houses in the road had different numbers of occupants, you could use an array such as the following to store their names:

String[][] houses = {
  { "Joey", "Tommy", "Podlington" },
  { "Pickles", "Scragg", "Floopy", "Zuppo" },
};

As you can see, the first house has three occupants, and the second has four. In other words, houses[0]. length will be three, and houses[1]. length will be four.

As with one-dimensional arrays, you don’t have to declare, define, and initialize an array at the same time. For example, you might not know how many occupants there are in the houses:

String[][] houses = new String[2][];

Here, you’ve told Java that the first dimension of the newHouses array will contain two elements that will both be arrays. When you declare a two-dimensional array, you do have to declare at least the first (primary) array dimension. You can then specify the second index before use. Again, you can create jagged arrays in this fashion, as in the following example:

String[][] houses = new String[2][];
houses[0] = new String[3];
houses[0][0] = "Joey";
houses[0][1] = "Tommy";
houses[0][2] = "Podlington";
houses[1] = new String[4];
houses[1][0] = "Pickles";
houses[1][1] = "Scragg";
houses[1][2] = "Floopy";
houses[1][3] = "Zuppo";

Now, you’ll finish the chapter by looking at a common task when working with arrays: sorting.

Sorting Arrays

The standard Java package java.util contains a class called Arrays that provides a range of methods for array handling. It has methods for searching an array, for comparing arrays, and so on. One method of this class, Arrays.sort(), provides a way of sorting the elements of an array. For full details of this class, see the documentation at http://java.sun.com/j2se/1.4.1/docs/api/java/util/Arrays.html.

The sort() method sorts an array according to the natural ordering of the data type held in the array. Natural ordering is the name given to the default ordering for a particular data type. For example, the natural order for integers is ascending numerical order, and for strings, it’s ascending alphabetic order.

For example, if you have an integer array containing five values:

int[] sortme = {25, 32, 19, 27, 24};

and you printed this array using a loop such as this:

for(int i = 0; i < sortme.length; i++)
  System.out.println(ages[i]);

then you’d expect to see the following:

25
32
19
27
24

However, if you first sort the array and then print it like this:

Arrays.sort(sortme);
for(int i = 0; i < sortme.length; i++)
  System.out.println(ages[i]);

then the values would appear lowest first:

19
24
25
27
32

So what if you need to sort something unnaturally? The java.util package also contains an interface called Comparator. If you implement the compare and equals methods of this interface, you could use another Arrays.sort() method, Arrays.sort(Object[] array, Comparator c), to order elements according to a different pattern. Implementing comparators is beyond the scope of this book, but if you want to find out more, you can find a good book at http://www.apress.com/ategory.html?nID=32.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials