tags:

views:

141

answers:

9

This is just a simple question, and I can't find the answer in the documentation !

String args[] = new String[0];
args[0] = "test";

Is that correct ? Does this creates an array with 1 element or 0 elements ?

Thank you, I know, stupid question, but I couldn't find the answer in the Java doc.

+5  A: 

This creates an array with length 0. The second line will give an ArrayIndexOutOfBoundsExpection.

Thirler
+3  A: 

Your code is wrong. The first number states the length of the array, so it should be 1

String args[] = new String[1];
nanda
+1  A: 

String[] arr = new String[]{"test"}

Gadolin
+1  A: 

Yep it does seem a little odd - when you create the array you are declaring how many elements the array will have so 0 means no elements. Yet when you traverse an array the first element is the 0th element not the 1st element... Just remember that size/length are not the same as index.

BigMac66
A: 

An array of length 5 is created with:

String myArray[] = new String[5];

The items in this array are indexed using 0, 1, 2, 3, 4 - note that they start at index 0, not index 1, and so go up to (array length - 1).

So

new String[0]

creates an array of length 0. Assigning to index 0 will cause an error - there are no positions in the array to assign to.

new String[1]

would create an array of length 1, with a single position at index 0, so you could then legally do:

myArray[0] = "happy days";
sje397
It makes technical sense for languages to have zero-based array indexes. However, this has induced an uncountable number of errors.
Tony Ennis
A: 

String args[] = new String[0];

This creates an array with no elements. Accessing any element, even args[0], would cause an ArrayIndexOutOfBoundsException. The number of components of the array is available in args.length.

String args[] = new String[1];

This creates an array with 1 element. The element is accessed as args[0]. The first element is always at index 0. Accessing any other element would cause an ArrayIndexOutOfBoundsException.

String args[] = new String[10];

This creates an array with 10 elements. First element is args[0] and the last element is args[9]. The last position is always one less than the size of the array.

References

Java tutorial on arrays:

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Spec:

http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

An array object contains a number of variables. The number of variables may be zero, in which case the array is said to be empty. The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

Bert F
A: 

new String[x] will create an empty array of Strings with size x. With x=0, your Array will have no entries, so any attempt to access its elements will result in an exception. If you want it to have one element, you should specify you want one element: new String[1] will create an Array of Strings with 1 entry.

While the above parameter specifies the size of the array, the one you use later is the index. In many languages, [] are used for both index (in regular use) and size (when creating arrays), which may be confusing.

Simple rule: a valid index will always be >= 0, and < size of the array.

0 <= index < size

An index, also called offset, is how far from the start you go - how many elements into the array you step.

foo
A: 

If you're trying to do the PHP-like equivalent of args[] = "new entry" then take a look at

List<String> args = new ArrayList<String>();
args.add("test");
args.add("and some more");
args.add("and even more");

This works fine, and will expand your List automatically. When you need to convert it to an array, you can use:

String[] argArray = args.toArray(new String[args.size()]);
Matthijs Bierman
A: 

String args[] = new String[0];

Creates an array of size 0 also called as an empty array. Since the array contains no elements, no index can be used on it including 0. Usage of any index on it leads to java.lang.ArrayIndexOutOfBoundsException.

codaddict