1

Im doing a thing for a thing and I have to "Multiply each element in list2 by its element index number and place the value in array list1." and so my code is

public static void main(String[] args) {        
        int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
        int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 
        int L1= 0;
        int L2= 0;

        for( L2=0 ;L2<List2.length;L2++) {
            List2[L2]= (int) (100*Math.random());
            System.out.print(" "+List2[L2]);
        }//end of list2 for loop

System.out.println("");

        for( L1 = 0;L1<List1.length;L1++)
        {
            List1[L1]= List2[L2]*L2;
            System.out.print(" "+List1[L1]);
        }//end of list1 for loop
)

and it throws this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20"

  • 3
    You're not updating L2 in the second loop which results in you accessing elements outside of the List2 array. L2 has a value of 20 there after the first loop. – Expert Thinker El Rey May 30 '20 at 15:24

4 Answers4

0

Your List1 actually contains 21 elements (mind the last ",", which adds another element) while List2 contains only 20. So, in the second loop, you're iterating 21 times, and when you get to the 21st, List2 contains no more elements.

Mario MG
  • 364
  • 2
  • 13
0

Your first list has 21 elements, your second list has 20 elements. In your second loop you access the 21 element of the second list. So you get the exception.

notice the comma at the end of your declaration of List1:

int List1 [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
int List2 [] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

You probably wanted to do the following:

int List1 [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int List2 [] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

But looking at your code - if you just want to initialize an array with empty values you just can do

int[] list = new int[20];

And you got other problems in your code. After your first loop L2 will always be the size of List2 (20). So in your second loop in List1[L1] = List2[L2] * L2; you are accessing the element at index 20 of List2 - that doesn't exist, because at size=20 your maximum index is 19.

So - I don't know what you try to achieve with your code, but it won't work this way.

TomStroemer
  • 1,390
  • 8
  • 28
0

You have an exception in your second loop. you try to call List2[20]. List2 only have 0 to 19 index available and you are trying to access 20th element which is not available.

List1[L1]= List2[L2]*L2;

Try like this.

List1[L1]= List2[L1]*L2;

complete code.

public static void main(String[] args) {
    int List1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    int List2[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
    int L1 = 0;
    int L2 = 0;

    for (L2 = 0; L2 < List2.length; L2++) {
        List2[L2] = (int) (100 * Math.random());
        System.out.print(" " + List2[L2]);
    } // end of list2 for loop

    System.out.println("");
    System.out.println(List1.length + " " + List2.length);

    for (L1 = 0; L1 < List1.length; L1++) {
        List1[L1] = List2[L1] * L2;
        System.out.print(" " + List1[L1]);
    } // end of list1 for loop
}
Natsu
  • 443
  • 3
  • 10
0

Because at line List1[L1]= List2[L2]xL2;* L2 = 20, but the max index of the array is 19. You must write:

public static void main(String[] args) {        
    int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
    int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 
    int L1= 0;
    int L2= 0;

    for( L2=0 ;L2<List2.length;L2++) {
        List2[L2]= (int) (100*Math.random());
        System.out.print(" "+List2[L2]);
    }//end of list2 for loop

    System.out.println("");

    for( L1 = 0;L1<List1.length;L1++)
    {
        List1[L1]= List2[L2 - 1]*L2; //Here the change
        System.out.print(" "+List1[L1]);
    }//end of list1 for loop
}

or

public static void main(String[] args) {        
    int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
    int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 
    int L1= 0;
    int L2= 0;

    for( L2=0 ;L2<List2.length;L2++) {
        List2[L2]= (int) (100*Math.random());
        System.out.print(" "+List2[L2]);
    }//end of list2 for loop

    System.out.println("");

    for( L1 = 0;L1<List1.length;L1++)
    {
        List1[L1]= List2[L1]*L2; //Here the change
        System.out.print(" "+List1[L1]);
    }//end of list1 for loop
}