0

For my Android development, I have 2 arrays which is very identical except for the first and last value as below.

<string-array name="minvalues">
    <item>Any Value</item>
    <item>100</item>
    <item>200</item>
    <item>300</item>
</string-array>

<string-array name="maxvalues">
    <item>100</item>
    <item>200</item>
    <item>300</item>
    <item>Any Value</item>
</string-array>

I'm thinking is there a way to define the common values i.e. 100, 200, 300 as another array and have the minvalues and maxvalues include it from it, so we could share the common values across minvalues and maxvalues. Is this possible in Android arrays xml?

1 Answer 1

0

I don't think there is, since the values are string, you will need to manually convert them to integers in order to look for common values using

String[] minValues = getResources().getStringArray(R.array.minvalues);
String[] maxValues = getResources().getStringArray(R.array.maxvalues);

and then convert them into int with

int[] minValsInt = Arrays.asList(minValues).stream().mapToInt(Integer::parseInt).toArray();
int[] maxValuesInt = Arrays.asList(maxValues).stream().mapToInt(Integer::parseInt).toArray();

now you can loop through each one and get the common values.

Edit Here's the way to get the duplicates from this post

void findDupes(int[] a, int[] b) {
HashSet<Integer> map = new HashSet<Integer>();
for (int i : a)
    map.add(i);
for (int i : b) {
    if (map.contains(i))
        // found duplicate!    
} 

}

Cheers

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

2 Comments

Android supports JDK 8, now?
Thanks snowman. Sorry for not making my question clear enough. My question is not finding the common, but not towards writing the common array, and have 2 arrays includes it, where by one will insert ANY as the first value, and the second will include ANY as the last value. I would like to do this in the resource XML. I have edited my question changing the "Decouple" word to "Define". Hopes this is clearer now. Thanks.

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.