# Calculate with Array

## Calculate Average value of Array elements

``````

public class Main {
public static void main(String[] args) {

int[] intArray = new int[] { 1, 2, 3, 4, 5 };
// calculate sum
int sum = 0;
for (int i = 0; i < intArray.length; i++){
sum = sum + intArray[i];
}
// calculate average
double average = sum / intArray.length;

System.out.println("average: " + average);
}
}

``````

The output:

``````
average: 3.0
``````

## Create Fibonacci Series with array

``````
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int length = 20;
long[] series = new long[length];
series[0] = 0;
series[1] = 1;
for (int i = 2; i < length; i++) {
series[i] = series[i - 1] + series[i - 2];
}
System.out.print(Arrays.toString(series));
}
}
``````

The output:

``````
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
``````

The following code use two-dimensional double type array to do Matrix calculation

``````
class Matrix {
private double[][] doubleArray;

Matrix(int nrows, int ncols) {
doubleArray = new double[nrows][ncols];
}

int getCols() {
return doubleArray[0].length;
}

int getRows() {
return doubleArray.length;
}

double getValue(int row, int col) {
return doubleArray[row][col];
}

void setValue(int row, int col, double value) {
doubleArray[row][col] = value;
}
}

public class Main {
public static void main(String[] args) {
Matrix a = new Matrix(1, 3);
a.setValue(0, 0, 1); // | 1 2 3 |
a.setValue(0, 1, 2);
a.setValue(0, 2, 3);
dump(a);
Matrix b = new Matrix(3, 2);
b.setValue(0, 0, 4); // | 4 7 |
b.setValue(1, 0, 5); // | 5 8 |
b.setValue(2, 0, 6); // | 6 9 |
b.setValue(0, 1, 7);
b.setValue(1, 1, 8);
b.setValue(2, 1, 9);
dump(b);
dump(multiply(a, b));
}

static void dump(Matrix m) {
for (int i = 0; i < m.getRows(); i++) {
for (int j = 0; j < m.getCols(); j++){
System.out.print(m.getValue(i, j) + " ");
}
System.out.println();
}
System.out.println();
}

static Matrix multiply(Matrix a, Matrix b) {
if (a.getCols() != b.getRows()) {
throw new IllegalArgumentException("rows/columns mismatch");
}
Matrix result = new Matrix(a.getRows(), b.getCols());
for (int i = 0; i < a.getRows(); i++) {
for (int j = 0; j < b.getCols(); j++) {
for (int k = 0; k < a.getCols(); k++) {
result.setValue(i, j, result.getValue(i, j) + a.getValue(i, k) * b.getValue(k, j));
}
}
}

return result;
}
}

``````
Home
Java Book
Language Basics

Array: