开发者

Printing Arrays in Java

开发者 https://www.devze.com 2023-02-16 11:00 出处:网络
This time I want to print an array from the end to the start. This is what I wrote: public class Arrays {

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开发者_如何学Go the Eclipse error notifactions. I'll be glad for assistance.


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.


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--){


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.


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 :)


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.


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--){


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.


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.


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.


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


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


.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


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


you starting from too high index:

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

ah i remember my tohna 1 second exercise :)

0

精彩评论

暂无评论...
验证码 换一张
取 消