3

I have two string arrays in my xml file. the code snippet is

<string-array name="ECE1NAME">
 <item>ENG1</item>
 <item>MAT1</item>
 <item>PHY1</item>
 <item>CHM1</item>
 <item>EG</item>
 <item>FOC</item>
 <item>CPL1</item>
 <item>EPL</item>
</string-array>
<string-array name="ECE2NAME">
 <item>ENG2</item>
 <item>CHM2</item>
 <item>PHY2</item>
 <item>MAT2</item>
 <item>ECED</item>
 <item>BCM</item>
 <item>PCL</item>
 <item>CPL2</item>
 <item>CDL</item>
</string-array>

Now I want to combine this into one string depending on the if condition in my class file. My java code snippet is

 if(messagec2.equals("1"))
 {
 sub=getResources().getStringArray(R.array.ECE1NAME);
 }
 if(messagec2.equals("2"))
 {
 sub=getResources().getStringArray(R.array.ECE1NAME);
 sub=getResources().getStringArray(R.array.ECE2NAME);
 }

In the second condition I want to put both the ECE1NAME and ECE2NAME string array into the same variable sub.Please help me.

Fabien Sa
9,5704 gold badges44 silver badges45 bronze badges
asked Nov 12, 2013 at 17:31
3
  • is both array values static? Commented Nov 12, 2013 at 17:32
  • why not create two temporary arrays and just merge them? Commented Nov 12, 2013 at 17:34
  • I am new to android programming.Please can u tell me how to merge with temporary arrays? Commented Nov 12, 2013 at 17:36

2 Answers 2

10

take stirng variables like string a[],string B[] and concatenate them like this by passing your values to this method..

private String[] concat(String[] A, String[] B) {
 int aLen = A.length;
 int bLen = B.length;
 String[] C= new String[aLen+bLen];
 System.arraycopy(A, 0, C, 0, aLen);
 System.arraycopy(B, 0, C, aLen, bLen);
 return C;
}
answered Nov 12, 2013 at 17:43
0

When the messagec2.equals("2") you're setting the sub variable and then in the next line you're replacing the value you just set. Try an arraylist to get the first value and then APPEND the value of the second array to it.

This thread should help. How can I concatenate two arrays in Java?

answered Nov 12, 2013 at 17:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.