0

I have a set of 1d arrays that are being pulled from a method (tableMethod) - i want to grab those 1d arrays, then sum all the elements in each 1d array. How can I do that?

I have two loops

  • one that can sum a 1d array by itself
  • another that can display all the 1d arrays

I'm having difficulty combining the for loops so that it can grab each 1d array, sum it, then move on to the next 1d array, sum it, and so forth

the result should look something like:

  • total: 391

  • total: 393

  • total: 3903

  • total: 39104

  • total: 39031 ... and so forth

      int sum = 0;
      int w = 0;
      int[] arrayOne = tableMethod(table, w);
    
      for (int k = 0; k < arrayOne.length; k++) {
          sum = sum + arrayOne[k];
      }
      for (int i = 0; i < arrayOne.length; i++) {
          System.out.println(Arrays.toString(tableMethod(table, i)));
      }
      System.out.println(sum);
    

    }

3
  • 3
    Where is the “set of arrays”? You have only one int[] array. Commented Oct 22, 2021 at 20:04
  • The set of arrays are in the method, the way I'm pulling them is by using table as the array and the "w" is the column numbers - so for example, i could call a tableMethod(table, 2) - which would pull the array then select the 2nd column Commented Oct 22, 2021 at 20:19
  • where's that table parameter defined? Is it a 2d array? Commented Oct 22, 2021 at 20:26

2 Answers 2

2

Something like this would work,

import java.util.stream.IntStream;

int n = <max value of w>
int sum = 0;

for (int i = 0; i < n; i++) {
   int[] array = tableMethod(table, i);
   int arr_sum = IntStream.of(array).sum();
   System.out.println(arr_sum); //single array sum

   sum += arr_sum;
}
System.out.println(sum); //total sum
Sign up to request clarification or add additional context in comments.

2 Comments

this is almost there! I had made a code similar to this. What I'm looking for is for the 1d arrays to be summed individually. The loop you provided sums the elements of first array, then adds the sum of the second array with the total of the first array, then the third array is summed with the total of the first and second array and so forth.
If i have these 4 arrays: {1, 2, 3, 10, 3, 4, 7, 8, 9} {33, 34, 5, 6, 7, 91, 3, 49,10}, {43, 44, 45, 46, 47, 41, 49, 93,34} {10,10,10,10,10,10,10,10,10} I just want the loop to provide the sum of each one: 47 238 442 100
0

The code should be simply using nested loops (inner loop to calculate a sum may be replaced with stream):

int n = ... ; // a number of 1d arrays
int total = 0;
for (int i = 0; i < n; i++) {
    int[] arr = tableMethod(table, i);
    int sum = Arrays.stream(arr).sum();
    System.out.println(sum);
    total += sum;
}
System.out.println(total);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.