-4

I have an work for school, I need to make array using int type numbers. I already assigned every value. If i try to run it, it shows that there is - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20.

   if (K==0) {
        Random r = new Random();
        for (i = 0; i < 20; i++)
            A[i] = r.nextInt() * 100 - 50;
    }
    else {
        for (i=0; i < 20; i++) { // It shows that problem is somewhere in 
            A[i] = A[i + 1] + K; // these 2 lines.
        }
        System.out.println("A:");
        for (i = 0; i < 20; i++)
reijatsu
  • 3
  • 3
  • I cant see how big is you A array , but I guess it has a length of 20(where max index is 19). So A[i+1] would resulting in overflow on the last iteration (when i=19 you will invoke A[19+1] ) – beatrice Oct 23 '16 at 10:36
  • @reijatsu your code is incomplete so we won't be able to help you – Ivan Oct 23 '16 at 10:42

4 Answers4

1

That means that you try to access A[20] even though it doesn't exist

When i equals 19, the following line becomes a problem:

A[i] = A[i + 1] + K;

Use 19 insead of 20 in your loop and it should be fine. You need to decide what to do with A[19] though, as it's value won't change.

  • int A[] = new int[20]; int K, i; int C; Scanner sc = new Scanner(System.in); – reijatsu Oct 23 '16 at 10:35
  • Like i said, i already assigned and created array. – reijatsu Oct 23 '16 at 10:36
  • 1
    @reijatsu: Yes. You've created an array for which the valid indexes are `0` through `19`, inclusive. The above is pointing out that you're then trying to use `A[20]`, which is out of bounds. – T.J. Crowder Oct 23 '16 at 10:37
  • @T.J.Crowder I don't know, some people have taken this idea of "downvoting strategically" a bit too seriously ;) I upvoted yours –  Oct 23 '16 at 10:41
  • 1
    It also happens a lot when people answer questions that are obvious dupes. – takendarkk Oct 23 '16 at 10:41
  • 2
    Thanks guys for the help, appreciate it. Understood the point of it now. :) – reijatsu Oct 23 '16 at 10:43
  • @takendarkk: Answers to dupes are not automatically not useful, just as dupes are not automatically not useful. – T.J. Crowder Oct 23 '16 at 10:43
  • Tell that to the voters, not me. Also, do you really think that logic fits this particular scenario? Is there something here that isn't in the dupe? You say people did this automatically but I would do it even after thinking about it. – takendarkk Oct 23 '16 at 10:44
  • @reijatsu you're welcome! Please consider accepting the answer if it worked –  Oct 23 '16 at 10:45
  • Better to use `A.length - 1` instead of 19. – Andy Turner Oct 23 '16 at 11:12
0

The error means you're attempting to access an index that doesn't exist. The array is A[0]...[1]...[lastIndex]. But you're attempting to access an index that doesn't exist.

A[0]...[1]...[lastIndex]. [anIndexThatDoesntExist]

If you want to assign a new value to (the last) index of A. i.e. A[20].. then do A[i] = A[i] + K

Yanny
  • 1
  • 2
-1
    for (i=0; i < 20; i++) { // It shows that problem is somewhere in 
        A[i] = A[i + 1] + K; // these 2 lines.

what is the result of i+1 when i=19?

if you want to acces behind the currrent index in a loop you must reduce the loops iterationcount by the access offset (1 in tis case).

Timothy Truckle
  • 12,232
  • 2
  • 22
  • 44
-2

You are trying to access A[20] in the line;

A[i] = A[i + i] + K;

That is why the exception is raised.

johnnyaug
  • 762
  • 5
  • 18
Shree Naath
  • 397
  • 2
  • 3
  • 15