Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java arrays

Hi,

I am trying below example

http://www.learnjavaonline.org/en/Arrays

public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        int length = numbers[3];
        char[] chars = new char[length];
        chars[numbers.length] = 'y';
        System.out.println("Done!");
    }
}



I get error as below even after changiing to 3 from 2 as below
int length=number[3]
from
int length=number[2]



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
      at Main.main(Main.java:4)

Please advice what is meaning of above exception and why i am getting and how to fix it.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Array indexes start at 0.
int[] numbers = {1, 2, 3};
        int length = numbers[2];
        char[] chars = new char[length];
        chars[numbers.length-1] = 'y';
        System.out.println("Done!");

Open in new window


'length' (somewhat confusingly) IS the real length, but when you use it as an array index, it will always be one position too long.
Avatar of gudii9

ASKER

int length = numbers[3];

when we pass this length to



does it take 3 or 4
. . . but there is *no* numbers[3] - only numbers[0], numbers[1], and numbers[2]. (these ARE your three positions).

That is how arrays work - the FIRST element is ALWAYS 0 - zero.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

I think variable name(length) is confusing which is same as method name.

I changed as below

public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        int lengthVar = numbers[3];
        char[] chars = new char[lengthVar];
        chars[numbers.length-1] = 'y';
        System.out.println("Done!");
    }
}

I still get same index out of bound exception. How to fix to so that Done! is printed to console. please advice
I still get same index out of bound exception. How to fix to so that Done! is printed to console. please advice
But we just did that in the first comment I made. You are going 'round in circles here!

You CAN'T do this :

int lengthVar = numbers[3];

because there is NO "numbers[3]" only numbers[0], numbers[1], and numbers[2].

Read this comment again : krakatoa2014-05-29 at 17:37:34ID: 40098766

and let me know why you are having trouble with it.
Tell us what you think the value of lengthVar is in the code here :

        int[] numbers = {47, 49, 51};
        int lengthVar = numbers[2];
Avatar of gudii9

ASKER

i see your point
It is 51

I modified code like below
public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        int lengthVar = numbers[2];
        System.out.println("length!"+lengthVar);
        char[] chars = new char[lengthVar];
        chars[numbers.length-1] = 'y';
        System.out.println("char[2] is-->"+chars[2]+"char[1] is-->"+chars[1]+"char[] is-->"+chars[0]);
        System.out.println("Done!");
    }
}

Open in new window


I got below output as belowlength!3
char[2] is-->ychar[1] is-->char[] is-->
Done!
Avatar of gudii9

ASKER

i modified still further
public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        int lengthVar = numbers[2];
        System.out.println("length!"+lengthVar);
        char[] chars = new char[lengthVar];
        System.out.println("numbers.length-->"+numbers.length);
        chars[numbers.length-1] = 'y';
        System.out.println("char[2] is-->"+chars[2]+"char[1] is-->"+chars[1]+"char[] is-->"+chars[0]);
        System.out.println("Done!");
    }

Open in new window


i got below output which makes more sense now
length!3
numbers.length-->3
char[2] is-->ychar[1] is-->char[] is-->
Done!
i see your point

Great.

Slightly odd concept, something starting at zero, but that is how it works.