Chapter 13. Arrays


If you have done any scientific or engineering programming you are familiar with using arrays. In Java, as in every other programming language, arrays are used for storing data. If you solve a system of equations or create a computational grid you will be using arrays. Since Java is a C-based language, there are some similarities between Java and C arrays. Java array indices start at 0. An element of a Java array can be accessed using the element index surrounded by brackets, [] .

There are some notable differences between Java and C arrays. Java arrays are objects. Memory for an array is allocated on the heap using the new keyword. Arrays implicitly extend from the Object class and can use Object class methods . Unlike other languages, the Java runtime will make sure that you don't try to use an inappropriate (too large or negative) array index. If you do, an exception is thrown.

Elements stored in an array can be either reference or primitive type variables. Arrays can only hold variables of the same type. Java arrays have a fixed length, set when the array is created and afterwards cannot be changed. If you need a collection whose size can be changed and/or can store elements of different types, you can use one of the specialized collection classes from the java.util package. These classes are discussed briefly at the end of this chapter.

In this chapter we will cover ”

  • One-dimensional arrays

  • Two-dimensional arrays

  • Arrays of more than two dimensions

  • Initializing array elements

  • Accessing array elements

  • Arrays as method arguments and return types

  • Array length

  • Collection classes in the Java API



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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