File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you add to Arrays together?

 
Corbin Blake
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to take an array i.e. A[] and array B[] and add them together.

for instance if array A contains "abcd" and array B contains "efgh" i want to add them together and print out "abcdefgh".

Can you use arraycopy? I am at a lost for the logic to do so.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a method in the Java API that will help with this, but you don't have to rely on that. In fact, it might take you less time to figure it out yourself. If you don't know where to start, why don't you write some ideas down on paper. Write out the steps that it will take to accomplish the task. Feel free to post your ideas here. This forum works very well for "thinking out loud", so to speak.

HTH

Layne
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corbin: By "adding together" do you mean that an array {a, b, c} added to
{b, c} would be {a, b, c, b, c} or just {a, b,c}?

I would simply find the length of each array and create a 3rd array of as long as the sum of the lengths of arrays 1 and 2.
Now iterate thru the indexes of the arrays 1 and 3 adding everything in array 1 to array 3.
Then iterate thru arrays 2 and 3 adding everything in array 2 to array 3. NOTE: You continue to fill the new array, you DO NOT overwrite what you already added.

If the new array is not supposed to have duplicates then you need to check for those when filling the new array or clean them out afterwards.

NOTE: If the things in these two arrays will aways be chars you could immediately create strings (using constructor) from the char arrays and just use the '+' function.
[ November 15, 2004: Message edited by: Elouise Kivineva ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the content of one array is to be copied into a second, they cannot simply be assigned to one another. For instance, the following statement will not copy contents of arrayA to arrayB i.e.

arrayB = arrayA;

If you try this in a program it will appear to work. But what actually happens is arrayB will reference the same memory as arrayA.

A new area of memory needs to be allocated for an additional array using the 'new' keywork, arrayB will then reference this. The contents of arrayA is then copied into arrayB by using a loop.

Example code:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic