• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you add to Arrays together?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic