public class main{
public static int arraySum(int[] arr){
int sum = 0;
for (int i : arr){
sum += i;
}
return sum;
}
public static int[] rowSums(int[][] Arr2D){
int[] sums = new int[Arr2D.length];
for (int i = 0; i < Arr2D.length; i ++){
sums[i] = arraySum(Arr2D[i]);
}
return sums;
}
public static boolean isDiverse(int[][] Arr2D){
int[] sums = rowSums(Arr2D);
for (int i = 0; i < sums.length; i ++){
for (int j = i + 1; j < sums.length; j ++){
if (sums[i] == sums[j]){
return false;
}
}
}
return true;
}
public static void main(String args[]){
int[] arr = new int[]{1, 3, 2, 7, 3};
System.out.println(arraySum(arr));
int[][] Arr2D = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 10, 8, 2, 1}
};
for (int i : rowSums(Arr2D)){
System.out.println(i);
}
System.out.println(rowSums(Arr2D));
System.out.println(isDiverse(Arr2D));
}
}
main.main(null)
16
16
32
28
28
[I@580f75c1
false
Learnings:
- How to properly define an array. Originally I wrote it as: int[] arr = [1, 3, 2, 7, 3]; but it should actually be int[] arr = new int[]{1, 3, 2, 7, 3};
- How to properly define a 2d Array
- When initializing an Array I have to put int the length of it
- In order to find the length of array I use .length as a property not a method.
- No .append(), have to just set the value which works because the length of the array is already set.
Reflection:
This FRQ had me working with 2d arrays. This was interesting to me, since I hadn’t had a lot of experience directly defining 2d arrays in Java before. I do feel very comfortable with this topic though and most of the trouble I had was with the syntax rather than anything conceptual.