-1

I have made a program (for teaching myself purposes) with threads, which is calculating multiplication of elements of an array , using 2 threads. But I get for both threads an ArrayIndexOutOfBoundsException. The error looks like this:

Exception in thread "Thread-1" Exception in thread "Thread-0" Executing multiplying of last2elements
          java.lang.ArrayIndexOutOfBoundsException: 1
          at FirstPart.run(FirstPart.java:12)
          java.lang.ArrayIndexOutOfBoundsException: 1
          at SecondPart.run(SecondPart.java:10)

Java Code :

FirstPart.java

public class FirstPart extends Thread {
    int multiply = 1;
    static int n;
    static int[] v = new int[n];

    public void run() {
        System.out.println("Executing multiplying of first "+MultiplyDemo.cap1+"elements"); 
        for(int i = 1; i <= MultiplyDemo.cap1; i++) {
            multiply = multiply * FirstPart.v[i];
            System.out.println("Multiplication is "+ multiply);
        }

    }
}

SecondPart.java:

public class SecondPart extends Thread {
    int multiply = 1;

    public void run() {
        System.out.println("Executing multiplying of last " + (FirstPart.n - MultiplyDemo.cap1) + "elements"); 

        for(int i = MultiplyDemo.cap1;i <= FirstPart.n; i++) {
            multiply=multiply*FirstPart.v[i];
            System.out.println("Multiplication is "+multiply);
        }
    }
}

MultiplyDemo.java:

import java.util.Scanner;
import java.util.Vector;


public class MultiplyDemo {

    public static int cap1;


    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("introduceti numarul de elemente din vector:");
        FirstPart.n=s.nextInt();

        int []v=new int[FirstPart.n];
        System.out.println("Elementele vectorului sunt");
        for(int i = 0; i < v.length; i++)
            v[i]=s.nextInt();         
        cap1=FirstPart.n / 2;

        FirstPart thread1=new FirstPart();
        SecondPart thread2=new SecondPart();

        thread1.start();
        thread2.start();

        try { // wait for completion of all thread and then sum
            thread1.join();
            thread2.join(); //wait for completion of MathCos object 
            double z = thread1.multiply + thread2.multiply;
            System.out.println("produsul elementelor este" +z);
        } 
        catch(InterruptedException IntExp) {
        }

    }
}
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
123123d
  • 195
  • 2
  • 14

5 Answers5

2

Arrays indexes are 0-based; if an array has a size of n valid indexes are in range 0..n-1.
All your for loops check with a <= instead of using < and this is why you are getting ArrayIndexOutOfBoundsException.

Usually a "for loop" on arrays has this form:

Object[] array = new Object[size];
for(int i = 0;i < array.length;i++) {
  ..
}

In this way you can never exceed the size of array

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • I understand what I am doing wrong, but could you please show me what am I supposed to do ? It would mean the world to me ! – 123123d Jul 10 '14 at 09:35
  • replace `i <= FirstPart.n` with `i < FirstPart.v.length` and `i <= MultiplyDemo.cap1` with `i < FirstPart.v.length` and you don't get exception anymore. – Luca Basso Ricci Jul 10 '14 at 09:38
  • Just a question: are your teaching yourself threads and you are in trouble writing a indexed loop on arrays? I think you should rethinking about your learning program steps... – Luca Basso Ricci Jul 10 '14 at 09:40
0

Make your both for loop to this :

for(int i=0; i < MultiplyDemo.cap1; i++)

And PS : Please post question in proper format so that it will be easy for someone to help with.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
  • but it won't do what is's supposed to , the SecondPart class should continue multiplying from the MultyplyDemo.cap1 until the end(second half of the array) – 123123d Jul 10 '14 at 08:44
0

MultiplyDemo.cap1 is bigger than you n leading to your for loop trying to access an index that does not exist:

for(int i=1;i<=MultiplyDemo.cap1;i++)
    multiply=multiply*FirstPart.v[i]; // Misses index 0 and tries to go to index n, when n-1 is max
Pphoenix
  • 1,423
  • 1
  • 15
  • 37
0

Your "n" defaults as 0, and so your v array has 0 length.

This is why you're getting index out of bounds.

In your main method, you're initalizing a new "v" variable. Keep in mind that this "v" array is not the same static "v" array as in "FirstPart"

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
0

FirstPart.v is still an empty array. You are assigning the value to n from the main() method and initializing values to a local array which doesn't affect FirstPart.v

Abe
  • 31
  • 4
  • but I don't know how to alter the code, if I move the n,v, declarations to my main I can't have acces to them I get errors. I am a beginner , so I don't know what should I do in this case. – 123123d Jul 10 '14 at 08:57