Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15324
  • Last Modified:

Java - Concat Arrays

Hi everyone,

i just would like to know how to concat two or more arrays:
i want to do somethign like this:

String[] sample1= whatever;
String[] sample2= whatever;
String[] sampletotal= sample1+ sample2;

Thankx for ur help.      
0
miraodb
Asked:
miraodb
  • 10
  • 9
  • 3
  • +4
1 Solution
 
zzynxSoftware engineerCommented:
List total = Arrays.asList(sample1).addAll( Arrays.asList(sample2) );
sampleTotal = (String[]) total.toArray();
0
 
mmuruganandamCommented:
int strlength = sample1.length + sample2.length;
String[] sampletotal = new String[strlength];

System.arraycopy(sample1, 0, sampletotal, 0, sample1.length);
System.arraycopy(sample2, 0, sampletotal, sample1.length, sample2.length];



0
 
zzynxSoftware engineerCommented:
still not asleep murugan?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
searlasCommented:
zzynx, I think this fails:  Arrays.asList(sample1).add...

Because the ArrayList is backed by the array, it cannot grow, and add/remove operators throw UnsupportedOperationException...
0
 
rama_krishna580Commented:
try this...

public class TestArrayConcat
{
    public static void main(String args[])
    {
       int[] array1 = { 1, 2, 3, 4, 5 };
       int[] array2 = new int[3];

      System.arraycopy(array1, 1, array2, 0, 3);

      System.out.println("array2 elements " + array2[0] + ", " + array2[1] + ", " + array2[2]);
  }
}

R.K.
0
 
maheshexpCommented:
i think

>>>System.arraycopy(sample2, 0, sampletotal, sample1.length, sample2.length];
should be System.arraycopy(sample2, 0, sampletotal, sample1.length + 1, sample2.length];
0
 
seanrowenCommented:
No, mmuruganandam was right -- the offset should be just sample1.length since that is the index of the first unfilled element of sampletotal.
0
 
zzynxSoftware engineerCommented:
>> zzynx, I think this fails:  Arrays.asList(sample1).add...
You could be right. I tried to write it too short.
This is how it had to be:

        String sample1[] = { "Item1", "Item2" };
        String sample2[] = { "Item3", "Item4" };

        List total = new ArrayList(Arrays.asList(sample1));
        total.addAll(Arrays.asList(sample2));
        String sampleTotal[] = new String[total.size()];
        sampleTotal = (String[]) total.toArray(sampleTotal);

Hi miraodb, still alive?
0
 
mmuruganandamCommented:
>>Hi miraodb, still alive?

come on zzynx..
0
 
zzynxSoftware engineerCommented:
>> come on zzynx..
What do you mean?
0
 
mmuruganandamCommented:
Nothing! just like that....
0
 
zzynxSoftware engineerCommented:
??????
8°/
0
 
miraodbAuthor Commented:
guyz i tried all those stuffs but i always got errors.
sorry for my late answer by the way, i was on week end ;) not at work anymore. but now i'm back inda game.

so here is what i tried mixing all your explanations:

ArrayList testfulllist= new ArrayList();
String testfull[] = new String[1000];
String[] test1= request.getParameterValues("drilldown");
String[] test2= request.getParameterValues("drilldown2");
testfulllist.add(drillDownFields);
testfulllist.add(drillDownFields2);

then i wan to have it as an array so i did:
testfulllist.toArray(testfull);

i have this basic error message and i don't know what to do:
Cause racine:
java.lang.ArrayStoreException
      at java.lang.System.arraycopy(Native Method)
      at java.util.ArrayList.toArray(ArrayList.java:301)


      miraodb
0
 
miraodbAuthor Commented:
i also tried with the arrycopy but didn't work neither.
damn arrays.....
0
 
seanrowenCommented:
Here:

testfulllist.add(drillDownFields);
testfulllist.add(drillDownFields2);

(I think you mean to use test1 and test2 as the arguments?)
What you have after these two lines is an ArrayList with two elements, each of which is a String[]; you do not have an ArrayList full of Strings.

Thus when you call toArray(), the method is trying to create a String[][] -- an array of those two String[] arrays -- but you are putting it into a String[]. Thus the exception.

Try this:

String[] test1= request.getParameterValues("drilldown");
String[] test2= request.getParameterValues("drilldown2")
String[] testfull = new String[test1.length + test2.length];
System.arraycopy(test1, 0, testfull, 0, test1.length);
System.arraycopy(test2, 0, testfull, test1.length, test2.length);
0
 
miraodbAuthor Commented:
the arraycopy that mmuruganandam give me doesn't work either i have this error:
java.lang.NullPointerException ......
0
 
zzynxSoftware engineerCommented:
My previous comment works. I tested it.
0
 
zzynxSoftware engineerCommented:
"translated" to your variables:

String[] test1= request.getParameterValues("drilldown");
String[] test2= request.getParameterValues("drilldown2")

List total = new ArrayList(Arrays.asList(test1));
total.addAll(Arrays.asList(test2));
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);

0
 
miraodbAuthor Commented:
i tried what u just said zzynx but i still have a null pointer error:
java.lang.NullPointerException
      at java.util.Arrays$ArrayList.(Arrays.java:2301)
      at java.util.Arrays.asList(Arrays.java:2287)



ok here is the code i used:

String[] drillDownFields = request.getParameterValues("drilldown");
String[] drillDownFields2 = request.getParameterValues("drilldown2");
      
List total = new ArrayList(Arrays.asList(drillDownFields));
total.addAll(Arrays.asList(drillDownFields2));
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);

then i just call a funtion with tesfull as parameter:
panel.applyDrilldown(testfull);


0
 
miraodbAuthor Commented:
actually i found a solution with loops. anyway thankx to u guyz, i will share the points to zzynnx and mmuruganandam who gave me the two ideas.

thankx
miraodb
0
 
miraodbAuthor Commented:
actually i couldn't share coz 20 is not enough to split. sorry zzynx, next time.
i'm sure i will have some other java question for u ;)

see ya.
0
 
zzynxSoftware engineerCommented:
If right after

         testfull = (String[]) total.toArray(testfull);

you add this code:

         for (int i=0; i<testfull.size(); i++)
              System.out.println( testfull[i] );

then does it print what you expect?

>>i still have a null pointer error
>>java.lang.NullPointerException
>>     at java.util.Arrays$ArrayList.(Arrays.java:2301)      // <<< Arrays.java is not your code
>>     at java.util.Arrays.asList(Arrays.java:2287)

Could you provide a more extended call stack, till it contains a line of your own code?
And then provide us that line (and maybe some more context before and after) of your code?
0
 
zzynxSoftware engineerCommented:
>> actually i couldn't share coz 20 is not enough to split. sorry zzynx, next time.
Incrementing the points to 40 before splitting was an option.
Anyway...
0
 
miraodbAuthor Commented:
sorry man i'm new in here.
but actually i don't understand it was working fine and then suddenly i have again the nullpointer error:
java.lang.NullPointerException
      at _0002fviewer_0002ejspviewer_jsp_183._jspService(_0002fviewer_0002ejspviewer_jsp_183.java:127)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

i really don't understand how come it works and then not....
really wierd.

by the way can i cancel the acceptance and increase in order to split??
0
 
miraodbAuthor Commented:
my code is:
String[] drillDownFields = request.getParameterValues("drilldown");
String[] drillDownFields2 = request.getParameterValues("drilldown2");
      
String[] drillDownFieldsFull = new String[drillDownFields.length + drillDownFields2.length];

for (int i = 0; i < drillDownFields.length; i++)
 drillDownFieldsFull[i] = drillDownFields[i];
for (int i = 0; i < drillDownFields2.length; i++)
 drillDownFieldsFull[drillDownFields.length + i] = drillDownFields2[i];

thej i tried this which should be the same:
System.arraycopy(drillDownFields, 0, drillDownFieldsFull, 0, drillDownFields.length);
System.arraycopy(drillDownFields2, 0, drillDownFieldsFull, drillDownFields.length, drillDownFields2.length);

and i got the nullpointer error, since then i put it back as it was (loops) but still the same error.

wierd
0
 
miraodbAuthor Commented:
zzynx i will create another one right now and u can just say hi and i will accept so u can get the points.
0
 
zzynxSoftware engineerCommented:
>> by the way can i cancel the acceptance and increase in order to split??
FYI: Yes, you can.
Leave a zero-point question in the EE Community Support Topic Area explaining what happened.
The Moderators will "unaccept" your selection and re-open the question.

But, don't bother. It's not worth the effort for 20 points.
0
 
searlasCommented:
Your NullPointerException was probably caused by not checking the right/enough boxes on the web page (therefore causing getParameterValues to return null...)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now