0

Вот такое исключение выдает jenkins

В eclipse все работает, исключений не кидает

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Part4 {
static int[] maxes;
static public int max1;
static int c = 0;
Part4(int length){
maxes = new int[length];
}
public static int job(int[] ar) {
    int max = ar[0];
    for (int i = 0; i < ar.length; i++) {
        if (max < ar[i]) {
            max = ar[i];
        }
        try {
            Thread.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
    }
    synchronized (maxes) {
        if(c<maxes.length) {
    maxes[c] = max;
    c++;
        }
        max1=max;
    }
    return max;
}
public void getMax() {
     max1=maxes[0];
     job(maxes);
}
public int getMaxNoThread(int [][]arr) {
int max=arr[0][0];
for(int i=0;i<arr.length;i++) {
    for(int j=0;j<arr[i].length;j++) {
        try {
        Thread.sleep(1);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        if (max < arr[i][j]) {
            max = arr[i][j];
        }
    }
}
return max;
}
public static int[][] arrayFromString(){
StringBuilder str=new StringBuilder(Util.readFile("part4.txt"));
String[] srt=str.toString().split(System.lineSeparator());
int[][] arr =new int [srt.length][];
Pattern p=Pattern.compile("(?mU)(\\d+) *");
for(int i=0;i<srt.length;i++) {
Matcher m=p.matcher(srt[i]);
int j=0;
while(m.find()) {
    j++;
}
arr[i]=new int[j];
j=0;
 m=p.matcher(srt[i]);
while(m.find()) {
    arr[i][j]=Integer.parseInt(m.group(1));
    j++;
}
}
return arr;
}
public static void main(String[] args){
    int[][] arr =arrayFromString();
    Part4 t = new Part4(arr.length);
    Thread[] thr = new Thread[arr.length] ;
    long before =System.currentTimeMillis();
    for (int i = 0; i < arr.length; i++) {
        thr[i] = new myThread(arr[i]);
        thr[i].start();
    }
    for (int i = 0; i < arr.length; i++) {
    try {
        thr[i].join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
    t.getMax();
long after=System.currentTimeMillis();
System.out.println(max1);
System.out.println(after-before);
before=System.currentTimeMillis();
int max =t.getMaxNoThread(arr);
after=System.currentTimeMillis();
System.out.println(max);
System.out.println(after-before);
}
static class myThread extends Thread {
    int[] ar;
    myThread(int[] ar) {
        this.ar = ar;
    }
    public void run() {
        Part4.job(ar);
        Thread.currentThread().interrupt();
    }
}
}

Вопрос в том как пофиксить , ибо не могу понять что не так

0

1 ответ 1

0

Ошибка означает, что в массиве у вас 2 элемента, а вы запрашиваете третий, которого нет.

2
  • Спасибо Заметил что мне не хватает длины массива) 11 фев 2020 в 9:43
  • @Максим, не за что. "Галочка" слева от ответа. )
    – Olmicron
    11 фев 2020 в 10:27

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.