0

I have 5 float arrays all of the same length. At any given time: all, none or some of the arrays may be null (uninitialized).

I am trying to create a function which will add up the values of the arrays and produce a final array with the summed up values in position.

Is there a way to only include the values from the populated arrays?

Thanks, m

3
  • Why can't you filter null arrays with if (a[i] != null) {for (j=0; j<n; j++) {res[j] += a[i][j];}} Commented Dec 8, 2011 at 12:53
  • Can you please clarify the following ? Is the array it self initialized? When you say : float[] myfloats = new float[5]; then the float array may seem not initialized but in fact the elements in the array are initialized with a default value 0. Commented Dec 8, 2011 at 12:56
  • some of the arrays may be unititialised at the time the method is called. if that array is unititialised then I want to exclude it from the sum. Commented Dec 8, 2011 at 13:00

5 Answers 5

2

You'd have to check for it. Something like:

/**
 * Assumes that all rows are the same length as the first row.
 */
public float[] addValues(float[][] values){
    float[] result = new float[values[0].length];
    for(float[] row : values){
        if(row != null){
            for(int i = 0; i < row.length; i++){
                result[i] += row[i];
            }
        }
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

float[] result = new float[values[0].length]; could throw a NullPointerException if values was null; the same is true of the for-loop
@G.Bach - yes, you're completely correct. This was a minimal example that should have worked with the issue described by the OP, though arguably with a few assumptions. I already +1'd your answer as it is a much more complete, "production-ready" example.
@ ziesemer you're right, your answer solves the OP's problem with very little code, so it's a good example to give as an explanation. Just wanted to add the info that it omitted defensive style for the sake of brevity.
1
public float[] addUp(float[][] yourArrays) {
    if(yourArrays == null)
        return null;

    int i, j;
    int arrayLength = 0;
    float[] toReturn = null;

    //get the length of the array that will be returned
    for(i = 0; i < yourArrays.length; i++) {
        if((yourArrays[i] != null) && yourArrays[i].length > arrayLength)
            arrayLength = yourArrays[i].length;
    }

    //now build the array that will be returned, but only if there
    //have been any values at all
    if(arrayLength != 0) {

        //create the array
        toReturn = new float[arrayLength];

        //fill it
        for(i = 0; i < yourArrays.length; i++) {
            for(j = 0; j < arrayLength; j++) {
                if((yourArrays[i] != null)
                        && (j < yourArrays[i].length)
                    toReturn[j] += yourArrays[i][j];
            }
        }
    }

    return toReturn;
}

This should do what you want; it selects only values from arrays that have been initialized, only up until the last element of each array (so you won't have an ArrayOutOfBoundsException) and only if there where any values at all.

Runtime is in O([length of the longest array] * [number of arrays]).

Return values are

  • null if none of the arrays were initialized or if the longest array that was initialized had a length of 0;
  • a float[] holding in each cell the sum of all values for which any of your initial arrays had something set.

Comments

0

First of all, array objects have a length, but array references don't. Only a reference can be null (which means that it refers to no object). Your initial problem description is therefore somewhat in error.

But in any case, you can trivially check if an array reference is null:

if (theArray != null) {
    // add in contribution from theArray
    ...

Comments

0

Can you use Float ?

List<Float> newArr = new ArrayList<Float>();
for (float[] arr : arrays) 
{
  if (arrf != null)
  {
    for (float number : arr) { newArr.add(number); }
  }
}
newArr.toArray(new Float[0]);

Comments

-1

yes you can create a final array by the sum size of all the arrays then you start a for for each array then if the value is not null you add it to the final

String[] a = new String[5];
        String[] b = new String[5];
        String[] c = new String[5];
        String[] d = new String[5];
        String[] e = new String[5];

        String[] finalarray = new String[a.length + b.length + c.length + d.length + e.length];

        int count = 0;
        for (int i = 0; i < a.length; i++) {
            if(a[i] != null){
                finalarray[count] = a[i];
                count++;
            }
        }
        for (int i = 0; i < b.length; i++) {
            if(b[i] != null){
                finalarray[count] = b[i];
                count++;
            }
        }
        for (int i = 0; i < c.length; i++) {
            if(c[i] != null){
                finalarray[count] = c[i];
                count++;
            }
        }
        for (int i = 0; i < d.length; i++) {
            if(d[i] != null){
                finalarray[count] = d[i];
                count++;
            }
        }
        for (int i = 0; i < e.length; i++) {
            if(e[i] != null){
                finalarray[count] = e[i];
                count++;
            }
        }

1 Comment

He wanted to do matrix math (sum the values) not concatenate the arrays.

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.