0

I thought this to bind two arrays from resource into one array:

Resource res=getResources();

final int[] one_array=res.getIntArray(R.array.first_array) + res.getIntArray(R.array.second_array);

But variable array can't be declared like that showing:

The operator + is undefined for the argument type(s) int[], int[]

Also I would like to bind two arrays from resource + one array into one array. In my thought, it should be:

Resource res=getResource();

final int[] one_array={ 1,2,3,4,5,res.getIntArray(R.array.first_array),res.getIntArray(R.array.second_array) };

But variable array can't be declared like that showing:

Multiple markers at this line
    - Type mismatch: cannot convert from 
     int[] to int

How can I achieve declaring one array by binding two arrays from resources and normal array? Is there another/alternative ways/solutions to bind arrays?

3 Answers 3

4

Try ArrayUtils.addAll

final int[] one_array = ArrayUtils.addAll(res.getIntArray(R.array.first_array), res.getIntArray(R.array.second_array);

+ operator will concat two string.

Sign up to request clarification or add additional context in comments.

Comments

0

Or, if you don't want to include the whole jar for just this operation, define your own helper method based on the addAll() source code. In the end, all it really does is System.arrayCopy() both arrays into one larger array.

/**
 * <p>Adds all the elements of the given arrays into a new array.</p>
 * <p>The new array contains all of the element of <code>array1</code> followed
 * by all of the elements <code>array2</code>. When an array is returned, it is always
 * a new array.</p>
 *
 * <pre>
 * ArrayUtils.addAll(array1, null)   = cloned copy of array1
 * ArrayUtils.addAll(null, array2)   = cloned copy of array2
 * ArrayUtils.addAll([], [])         = []
 * </pre>
 *
 * @param array1  the first array whose elements are added to the new array.
 * @param array2  the second array whose elements are added to the new array.
 * @return The new int[] array.
 * @since 2.1
 */
public static int[] addAll(int[] array1, int[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }
    int[] joinedArray = new int[array1.length + array2.length];
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
}

Source (Apache 2.0 license).

2 Comments

what if three parameters are required? I want to add three arrays. @MH
@InnkuanThute: Just follow the same pattern as outlined above. In short, the most important part will be adding another System.arrayCopy() call for the third array. Even better: make the method work on any number of arrays. I'll leave that as a homework excersize up to you. :)
0

maybe default jdk don't use ArrayUtils class
if you want use default jdk to bind array use below code.

    int a[] = new int[11];
    int b[] = new int[21];
    int c[] = new int[a.length + b.length];

    System.arraycopy(a, 0, c, 0, a.length);
    System.arraycopy(b, 0, c, a.length, b.length);

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.