Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Array initialization


+ Font mai mare | - Font mai mic


Trimite pe Messenger
String operator +
Using Java operators
Network And Sockets
Windowing Components And Layout Classes
Coding style
Java 1.1 IO streams: Sources and sinks of data
Your first Java program
You never need to destroy an object
The Component Class

Array initialization

Initializing arrays in C is error-prone and tedious. C++ uses aggregate initialization to make it much safer. Java has no “aggregates” like C++, since everything is an object in Java. It does have arrays, and these are supported with array initialization.

An array is simply a sequence of either objects or primitives, all the same type and packaged together under one identifier name. Arrays are defined and used with the square-brackets indexing operator [ ]. To define an array you simply follow your type name with empty square brackets:

int[] a1;

You can also put the square brackets after the identifier to produce exactly the same meaning:

int a1[];

This conforms to expectations from C and C++ programmers. The former style, however, is probably a more sensible syntax, since it says that the type is “an int array.” That style will be used in this book.

The compiler doesn’t allow you to tell it how big the array is. This brings us back to that issue of “handles.” All that you have at this point is a handle to an array, and there’s been no space allocated for the array. To create storage for the array you must write an initialization expression. For arrays, initialization can appear anywhere in your code, but you can also use a special kind of initialization expression that must occur at the point where the array is created. This special initialization is a set of values surrounded by curly braces. The storage allocation (the equivalent of using new) is taken care of by the compiler in this case. For example:

int[] a1 = ;

So why would you ever define an array handle without an array?

int[] a2;

Well, it’s possible to assign one array to another in Java, so you can say:

a2 = a1;

What you’re really doing is copying a handle, as demonstrated here:


// Arrays of primitives.

public class Arrays ;

int[] a2;

a2 = a1;

for(int i = 0; i < a2.length; i++)


for(int i = 0; i < a1.length; i++)

prt('a1[' + i + '] = ' + a1[i]);


static void prt(String s)

You can see that a1 is given an initialization value while a2 is not; a2 is assigned later – in this case, to another array.

There’s something new here: all arrays have an intrinsic member (whether they’re arrays of objects or arrays of primitives) that you can query – but not change – to tell you how many elements there are in the array. This member is length. Since arrays in Java, like C and C++, start counting from element zero, the largest element you can index is length - 1. If you go out of bounds, C and C++ quietly accept this and allow you to stomp all over your memory, which is the source of many infamous bugs. However, Java protects you against such problems by causing a run-time error (an exception, the subject of Chapter 9) if you step out of bounds. Of course, checking every array access costs time and code and there’s no way to turn it off, which means that array accesses might be a source of inefficiency in your program if they occur at a critical juncture. For Internet security and programmer productivity, the Java designers thought that this was a worthwhile tradeoff.

What if you don’t know how many elements you’re going to need in your array while you’re writing the program? You simply use new to create the elements in the array. Here, new works even though it’s creating an array of primitives (new won’t create a non-array primitive):


// Creating arrays with new.

import java.util.*;

public class ArrayNew

public static void main(String[] args)

static void prt(String s)

Since the size of the array is chosen at random (using the pRand( ) method defined earlier), it’s clear that array creation is actually happening at run-time. In addition, you’ll see from the output of this program that array elements of primitive types are automatically initialized to ”empty” values. (For numerics, this is zero, for char, it’s null, and for boolean, it's false.)

Of course, the array could also have been defined and initialized in the same statement:

int[] a = new int[pRand(20)];

If you’re dealing with an array of non-primitive objects, you must always use new. Here, the handle issue comes up again because what you create is an array of handles. Consider the wrapper type Integer, which is a class and not a primitive:


// Creating an array of non-primitive objects.

import java.util.*;

public class ArrayClassObj

public static void main(String[] args)


static void prt(String s)

Here, even after new is called to create the array:

Integer[] a = new Integer[pRand(20)];

it’s only an array of handles, and not until the handle itself is initialized by creating a new Integer object is the initialization complete:

a[i] = new Integer(pRand(500));

If you forget to create the object, however, you’ll get an exception at run-time when you try to read the empty array location.

Take a look at the formation of the String object inside the print statements. You can see that the handle to the Integer object is automatically converted to produce a String representing the value inside the object.

It’s also possible to initialize arrays of objects using the curly-brace-enclosed list. There are two forms, the first of which is the only one allowed in Java 1.0. The second (equivalent) form is allowed starting with Java 1.1


// Array initialization

public class ArrayInit ;

// Java 1.1 only:

Integer[] b = new Integer[] ;


This is useful at times, but it’s more limited since the size of the array is determined at compile time. The final comma in the list of initializers is optional. (This feature makes for easier maintenance of long lists.)

The second form of array initialization, added in Java 1.1, provides a convenient syntax to create and call methods that can produce the same effect as C’s variable argument lists (known as “varargs” in C). These included, if you choose, unknown quantity of arguments as well as unknown type. Since all classes are ultimately inherited from the common root class Object, you can create a method that takes an array of Object and call it like this:


// Using the Java 1.1 array syntax to create

// variable argument lists

class A

public class VarArgs

public static void main(String[] args) );

f(new Object[] );

f(new Object[] );


At this point, there’s not much you can do with these unknown objects, and this program uses the automatic String conversion to do something useful with each Object. In Chapter 11 (run-time type identification or RTTI) you’ll learn how to discover the exact type of such objects so that you can do something more interesting with them.

See Thinking in C++ for a complete description of aggregate initialization.

Politica de confidentialitate



Vizualizari: 610
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site