Link to home
Start Free TrialLog in
Avatar of miraodb
miraodb

asked on

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.      
Avatar of zzynx
zzynx
Flag of Belgium image

List total = Arrays.asList(sample1).addAll( Arrays.asList(sample2) );
sampleTotal = (String[]) total.toArray();
ASKER CERTIFIED SOLUTION
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still not asleep murugan?
Avatar of searlas
searlas

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...
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.
i think

>>>System.arraycopy(sample2, 0, sampletotal, sample1.length, sample2.length];
should be System.arraycopy(sample2, 0, sampletotal, sample1.length + 1, sample2.length];
No, mmuruganandam was right -- the offset should be just sample1.length since that is the index of the first unfilled element of sampletotal.
>> 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?
>>Hi miraodb, still alive?

come on zzynx..
>> come on zzynx..
What do you mean?
Nothing! just like that....
??????
8°/
Avatar of miraodb

ASKER

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
Avatar of miraodb

ASKER

i also tried with the arrycopy but didn't work neither.
damn arrays.....
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);
Avatar of miraodb

ASKER

the arraycopy that mmuruganandam give me doesn't work either i have this error:
java.lang.NullPointerException ......
My previous comment works. I tested it.
"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);

Avatar of miraodb

ASKER

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);


Avatar of miraodb

ASKER

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
Avatar of miraodb

ASKER

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.
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?
>> 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...
Avatar of miraodb

ASKER

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??
Avatar of miraodb

ASKER

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
Avatar of miraodb

ASKER

zzynx i will create another one right now and u can just say hi and i will accept so u can get the points.
>> 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.
Your NullPointerException was probably caused by not checking the right/enough boxes on the web page (therefore causing getParameterValues to return null...)