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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
still not asleep murugan?
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 UnsupportedOperationExcept ion...
Because the ArrayList is backed by the array, it cannot grow, and add/remove operators throw UnsupportedOperationExcept
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.
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
}
}
R.K.
i think
>>>System.arraycopy(sample 2, 0, sampletotal, sample1.length, sample2.length];
should be System.arraycopy(sample2, 0, sampletotal, sample1.length + 1, sample2.length];
>>>System.arraycopy(sample
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(sa mple1));
total.addAll(Arrays.asList (sample2)) ;
String sampleTotal[] = new String[total.size()];
sampleTotal = (String[]) total.toArray(sampleTotal) ;
Hi miraodb, still alive?
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(sa
total.addAll(Arrays.asList
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..
>> come on zzynx..
What do you mean?
What do you mean?
Nothing! just like that....
??????
8°/
8°/
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 ("drilldow n");
String[] test2= request.getParameterValues ("drilldow n2");
testfulllist.add(drillDown Fields);
testfulllist.add(drillDown Fields2);
then i wan to have it as an array so i did:
testfulllist.toArray(testf ull);
i have this basic error message and i don't know what to do:
Cause racine:
java.lang.ArrayStoreExcept ion
at java.lang.System.arraycopy (Native Method)
at java.util.ArrayList.toArra y(ArrayLis t.java:301 )
miraodb
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
String[] test2= request.getParameterValues
testfulllist.add(drillDown
testfulllist.add(drillDown
then i wan to have it as an array so i did:
testfulllist.toArray(testf
i have this basic error message and i don't know what to do:
Cause racine:
java.lang.ArrayStoreExcept
at java.lang.System.arraycopy
at java.util.ArrayList.toArra
miraodb
ASKER
i also tried with the arrycopy but didn't work neither.
damn arrays.....
damn arrays.....
Here:
testfulllist.add(drillDown Fields);
testfulllist.add(drillDown Fields2);
(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 ("drilldow n");
String[] test2= request.getParameterValues ("drilldow n2")
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);
testfulllist.add(drillDown
testfulllist.add(drillDown
(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
String[] test2= request.getParameterValues
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);
ASKER
the arraycopy that mmuruganandam give me doesn't work either i have this error:
java.lang.NullPointerExcep tion ......
java.lang.NullPointerExcep
My previous comment works. I tested it.
"translated" to your variables:
String[] test1= request.getParameterValues ("drilldow n");
String[] test2= request.getParameterValues ("drilldow n2")
List total = new ArrayList(Arrays.asList(te st1));
total.addAll(Arrays.asList (test2));
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);
String[] test1= request.getParameterValues
String[] test2= request.getParameterValues
List total = new ArrayList(Arrays.asList(te
total.addAll(Arrays.asList
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);
ASKER
i tried what u just said zzynx but i still have a null pointer error:
java.lang.NullPointerExcep tion
at java.util.Arrays$ArrayList .(Arrays.j ava:2301)
at java.util.Arrays.asList(Ar rays.java: 2287)
ok here is the code i used:
String[] drillDownFields = request.getParameterValues ("drilldow n");
String[] drillDownFields2 = request.getParameterValues ("drilldow n2");
List total = new ArrayList(Arrays.asList(dr illDownFie lds));
total.addAll(Arrays.asList (drillDown Fields2));
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);
then i just call a funtion with tesfull as parameter:
panel.applyDrilldown(testf ull);
java.lang.NullPointerExcep
at java.util.Arrays$ArrayList
at java.util.Arrays.asList(Ar
ok here is the code i used:
String[] drillDownFields = request.getParameterValues
String[] drillDownFields2 = request.getParameterValues
List total = new ArrayList(Arrays.asList(dr
total.addAll(Arrays.asList
String testfull[] = new String[total.size()];
testfull = (String[]) total.toArray(testfull);
then i just call a funtion with tesfull as parameter:
panel.applyDrilldown(testf
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
thankx
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.
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.NullPointerExc eption
>> at java.util.Arrays$ArrayList .(Arrays.j ava:2301) // <<< Arrays.java is not your code
>> at java.util.Arrays.asList(Ar rays.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?
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.NullPointerExc
>> at java.util.Arrays$ArrayList
>> at java.util.Arrays.asList(Ar
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...
Incrementing the points to 40 before splitting was an option.
Anyway...
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.NullPointerExcep tion
at _0002fviewer_0002ejspviewe r_jsp_183. _jspServic e(_0002fvi ewer_0002e jspviewer_ jsp_183.ja va:127)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:119 )
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.jasper.servlet. JspServlet $JspCounte dServlet.s ervice(Jsp Servlet.ja va:130)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.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??
but actually i don't understand it was working fine and then suddenly i have again the nullpointer error:
java.lang.NullPointerExcep
at _0002fviewer_0002ejspviewe
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
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??
ASKER
my code is:
String[] drillDownFields = request.getParameterValues ("drilldow n");
String[] drillDownFields2 = request.getParameterValues ("drilldow n2");
String[] drillDownFieldsFull = new String[drillDownFields.len gth + drillDownFields2.length];
for (int i = 0; i < drillDownFields.length; i++)
drillDownFieldsFull[i] = drillDownFields[i];
for (int i = 0; i < drillDownFields2.length; i++)
drillDownFieldsFull[drillD ownFields. length + i] = drillDownFields2[i];
thej i tried this which should be the same:
System.arraycopy(drillDown Fields, 0, drillDownFieldsFull, 0, drillDownFields.length);
System.arraycopy(drillDown Fields2, 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
String[] drillDownFields = request.getParameterValues
String[] drillDownFields2 = request.getParameterValues
String[] drillDownFieldsFull = new String[drillDownFields.len
for (int i = 0; i < drillDownFields.length; i++)
drillDownFieldsFull[i] = drillDownFields[i];
for (int i = 0; i < drillDownFields2.length; i++)
drillDownFieldsFull[drillD
thej i tried this which should be the same:
System.arraycopy(drillDown
System.arraycopy(drillDown
and i got the nullpointer error, since then i put it back as it was (loops) but still the same error.
wierd
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.
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...)
sampleTotal = (String[]) total.toArray();