Unknown user
Unknown user

Reputation: 45301

Printing Arrays in Java

This time I want to print an array from the end to the start.

This is what I wrote:

public class Arrays {
public static void main (String[] args){
    for (int i = args.length; i >=0; i--){
        System.out.print(args[i]+" ");
    }
}

and this is the error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Assignment02Q04.main(Assignment02Q04.java:5).

Still having a hard time to realize the Eclipse error notifactions. I'll be glad for assistance.

Upvotes: 2

Views: 4571

Answers (14)

Dave McClelland
Dave McClelland

Reputation: 3413

.length of an array gives the count of elements in the array (starting at 1), but array indices start at 0, so the first iteration tries to access args[4] when the last element is actually args[3] and the size of the array is 4.

Change your code to:

for(int i = args.length - 1; i >= 0; i--)

and you'll be fine

Upvotes: 3

MoonBun
MoonBun

Reputation: 4402

you starting from too high index:

just replace i = args.length with i = args.length - 1

ah i remember my tohna 1 second exercise :)

Upvotes: 1

Eran Medan
Eran Medan

Reputation: 45715

All the other answers are correct, but here is another way to do so in Java (same complexity order)

    List<String> asList = Arrays.asList(args);
    Collections.reverse(asList);
    for (String arg : asList) {
        System.out.println(arg + " ");
    }

It traverses twice instead of once, but if you need the array to stay reversed for later use, this is better

Upvotes: 3

Max Komarychev
Max Komarychev

Reputation: 2856

In java array's indexes starts from 0. For example args contains 4 elemens, their indexes will be 0,1,2,3, but agrs.length is 4. You trying to get element, that lies beyond the array.

int i = args.length - 1 // will work

Upvotes: 3

Michael Berry
Michael Berry

Reputation: 72254

Close! It's just this line:

for (int i = args.length; i >=0; i--) {

That needs to change to:

for (int i = args.length-1; i >=0; i--) {

Why? Arrays in Java (and most languages) start at 0 and end at length-1. So if you've got an array of length 3, the valid indexes will be 0, 1 and 2. The index at length 3 will be invalid and thus cause an exception, which is what Java is complaining about.

Upvotes: 3

DaveJohnston
DaveJohnston

Reputation: 10151

If you are going to do your loop that way round, you will have to start at args.length - 1 Think about it like this, if you have an array with a single element length will return 1, and the only accessible index will be 0. Base on your code you will start at index 1, hence the ArrayIndexOutOfBoundsException.

Upvotes: 3

Ilya Saunkin
Ilya Saunkin

Reputation: 19800

Run your loop as (this is the foreach syntax)

for(String s : args) {
    System.out.print(s + " ");
}

instead. The array enumeration starts with 0 and ends with array.length - 1. The exception also tells you what was the element number when exception was raised.

Upvotes: 3

Nivas
Nivas

Reputation: 18344

In java arrays start with 0. So an array of length 5 has elements with index 0 to 4

The following statement

for (int i = args.length; i >=0; i--)

loops from 5 to 0 (for an array of size 5)

Change it to

for (int i = args.length-1; i >=0; i--)

and bingo!

PS: Actually you did loop till 0, so you probably already knew that arrays start at 0.

Upvotes: 10

Daniel Engmann
Daniel Engmann

Reputation: 2850

Array index starts at 0. So the last index is length-1.

When you have an array with 5 elements then the last has the index 4.

Your loop have to be

for (int i = args.length-1; i >=0; i--){

Upvotes: 3

Vadiklk
Vadiklk

Reputation: 3764

You can access till the (length - 1) in an array

An array which is {a, b, c}, a is indexed 0, b is 1, c is 2. Length is 3 but you cant access the array at the 3rd place.

Upvotes: 3

The GiG
The GiG

Reputation: 2611

You did args(args.length) thats invalid, you went to the place after the last in the array. an starts at 0 and goes untill length - 1

Upvotes: 2

Sorin
Sorin

Reputation: 1975

Since your array starts at index 0, then the last element is on the position args.length - 1. You are trying to acces the element at array.length, hence the ArrayIndexOutOfBoundsException.

Just change int i = args.length to int i = args.length - 1.

By the way, those are not "Eclipse error notifactions", they are Java Exceptions :)

Upvotes: 3

Adam Batkin
Adam Batkin

Reputation: 52984

You are almost there. You need to start with int i = args.length - 1 since arrays are indexed starting from 0, and the last element of the array is always the length minus 1.

Upvotes: 3

kvista
kvista

Reputation: 5059

Java uses 0 indexing for arrays, so your args.length needs to take that into account; you should start at one before:

for (int i = args.length-1; i >=0; i--){

Upvotes: 3

Related Questions