Given a link list of n floating-point numbers, it returns a two-dimensional link list, say M, of size n × n in which the entry
M[i][j]fori ≤ jcontains the average of the array entriesA[i]throughA[j]. That is: ifi ≤ j, thenM[i][j] = (A[i] + · · · + A[j])/( j − i + 1), whereas fori > jwe have thatM[i][j] = 0.
I have already designed this task in Array but Now I want to do it in Arraylist.
public static void main(String[] args) {
System.out.println("Enter Element");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
float[][] a = new float[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j]= sc.nextInt();
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j) {
a[i][j]=0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < j) {
a[i][j] = 0;
for (int k = i; k <= j; k++) {
a[i][j] += a[k][k];
}
a[i][j] /= (j - i + 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(" "+a[i][j]);
}
System.out.println("");
}
}
I have tried with the same idea but I am getting at this stage OutOfBoundException
for (int i = 0; i < lis.length; i++) {
for (int j = 0; j < lis.length; j++) {
if (i > j) {
lis[i][j].add(j, zero);
}
}
}