-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeDecimalArray.java
More file actions
73 lines (64 loc) · 1.91 KB
/
ThreeDecimalArray.java
File metadata and controls
73 lines (64 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ThreeDecimalArray;
import java.util.Random;
public class ThreeDecimalArray {
int x;
int y;
int z;
int[][][] array;
public ThreeDecimalArray(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
array = new int[x][y][z];
}
public void createArray(){
Random random = new Random();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
array[i][j][k] = random.nextInt(0,9);
}
}
}
}
public void printArray(){
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
System.out.print(array[i][j][k] + " ");
}
System.out.println();
}
System.out.println("Sum of elements of edge " + (i + 1) + " is " + arraySum(array[i]));
System.out.println();
}
}
public static int arraySum(int[][] array){
int sum = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
return sum;
}
public void sortArray(){
int[][] buffer = null;
boolean unSorted = true;
while (unSorted){
unSorted = false;
for (int i = 0; i < x - 1; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
if(arraySum(array[i]) > arraySum(array[i + 1])){
buffer = array[i];
array[i] = array[i + 1];
array[i + 1] = buffer;
unSorted = true;
}
}
}
}
}
}
}