Java-Threads ArrayIndexOutOfBoundsException

时间:2014-07-10 08:39:46

标签: java arrays multithreading exception indexoutofboundsexception

我用线程制作了一个程序(用于教我自己的目的),它使用2个线程计算数组元素的乘法。但我得到两个线程 ArrayIndexOutOfBoundsException 。错误如下所示:

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代码:

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

    }
}

5 个答案:

答案 0 :(得分:2)

数组索引是从0开始的;如果数组的大小为n,则有效索引的范围为0..n-1 您的所有for循环都会使用<=检查,而不是使用<,这就是您获得ArrayIndexOutOfBoundsException的原因。

通常,数组上的“for循环”具有以下形式:

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

这样你就永远不会超过数组的大小

答案 1 :(得分:0)

让你们两个循环到这个:

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

和PS:请以适当的格式发布问题,以便有人帮助。

答案 2 :(得分:0)

MultiplyDemo.cap1比您n大,导致您的for循环尝试访问不存在的索引:

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

答案 3 :(得分:0)

你的&#34; n&#34;默认为0,因此你的v数组长度为0。

这就是为什么你的索引超出范围。

在您的主要方法中,您将重新注册新的&#34; v&#34;变量。请记住,这&#34; v&#34;数组不是静态&#34; v&#34;数组如&#34; FirstPart&#34;

答案 4 :(得分:0)

FirstPart.v仍然是一个空数组。您正在从main()方法将值赋值给n并将值初始化为不影响FirstPart.v的本地数组