Solved

Java - Concat Arrays

Posted on 2004-04-23
28
15,243 Views
Last Modified: 2011-08-18
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
Comment
Question by:miraodb
  • 10
  • 9
  • 3
  • +4
28 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
List total = Arrays.asList(sample1).addAll( Arrays.asList(sample2) );
sampleTotal = (String[]) total.toArray();
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 20 total points
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
still not asleep murugan?
0
 
LVL 7

Expert Comment

by:searlas
Comment Utility
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
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
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
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
i think

>>>System.arraycopy(sample2, 0, sampletotal, sample1.length, sample2.length];
should be System.arraycopy(sample2, 0, sampletotal, sample1.length + 1, sample2.length];
0
 
LVL 1

Expert Comment

by:seanrowen
Comment Utility
No, mmuruganandam was right -- the offset should be just sample1.length since that is the index of the first unfilled element of sampletotal.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
>>Hi miraodb, still alive?

come on zzynx..
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> come on zzynx..
What do you mean?
0
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
Nothing! just like that....
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
??????
8°/
0
 

Author Comment

by:miraodb
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
i also tried with the arrycopy but didn't work neither.
damn arrays.....
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:seanrowen
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
the arraycopy that mmuruganandam give me doesn't work either i have this error:
java.lang.NullPointerException ......
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
My previous comment works. I tested it.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
"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
 

Author Comment

by:miraodb
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
 

Author Comment

by:miraodb
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
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
 

Author Comment

by:miraodb
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
 
LVL 7

Expert Comment

by:searlas
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now