Solved

Java - Concat Arrays

Posted on 2004-04-23
28
15,264 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
ID: 10900400
List total = Arrays.asList(sample1).addAll( Arrays.asList(sample2) );
sampleTotal = (String[]) total.toArray();
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 20 total points
ID: 10900410
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
ID: 10900462
still not asleep murugan?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 7

Expert Comment

by:searlas
ID: 10900632
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
ID: 10900686
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
ID: 10901103
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
ID: 10903276
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
ID: 10916840
>> 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
ID: 10916857
>>Hi miraodb, still alive?

come on zzynx..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10916903
>> come on zzynx..
What do you mean?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10917018
Nothing! just like that....
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10917283
??????
8°/
0
 

Author Comment

by:miraodb
ID: 10918104
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
ID: 10918173
i also tried with the arrycopy but didn't work neither.
damn arrays.....
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10918446
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
ID: 10918485
the arraycopy that mmuruganandam give me doesn't work either i have this error:
java.lang.NullPointerException ......
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10918564
My previous comment works. I tested it.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10918615
"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
ID: 10918682
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
ID: 10918958
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
ID: 10918981
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
ID: 10919017
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
ID: 10919038
>> 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
ID: 10919118
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
ID: 10919137
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
ID: 10919150
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
ID: 10919279
>> 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
ID: 10919299
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 60
java example issue 5 26
What is the use of Forwarding Class in java 1 18
Coding for the first time 9 36
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 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