Matthieu Napoli
Matthieu Napoli

Reputation: 49713

Java array creation

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.

Upvotes: 3

Views: 18531

Answers (10)

nanda
nanda

Reputation: 24808

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

String args[] = new String[1];

The first element in an array is labeled as myArray[0]

Upvotes: 3

Bert F
Bert F

Reputation: 87603

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:

Upvotes: 0

easoncxz
easoncxz

Reputation: 261

here's a simple & official example.

Upvotes: -2

codaddict
codaddict

Reputation: 455440

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.

Upvotes: 0

Matthijs Bierman
Matthijs Bierman

Reputation: 1757

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()]);

Upvotes: 2

foo
foo

Reputation: 2141

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.

Upvotes: 0

sje397
sje397

Reputation: 41862

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";

Upvotes: 0

BigMac66
BigMac66

Reputation: 1538

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.

Upvotes: 1

Gadolin
Gadolin

Reputation: 2686

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

Upvotes: 8

Thirler
Thirler

Reputation: 20760

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

Upvotes: 7

Related Questions