• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

How do I concatenate object arrays on the fly?

I have a method that returns an array of objects, this method is called for each item in another array from within a for-loop. I wish to add the new array returned by each of these calls to a single array.
0
ScottRogersGC
Asked:
ScottRogersGC
1 Solution
 
ellandrdCommented:
class Merger
{
      private int[] arr;
 
      public Merger(int[] pa, int[] pb) {
            arr = new int[pa.length + pb.length];
            for (int x=0; x < pa.length; x++) {
                  arr[x] = pa[x];
            }
            for (int x=0; x < pb.length; x++) {
                  arr[x+pa.length] = pb[x];
            }
      }
 
      public int[] getArray() {
            return arr;
      }
}
 
public class MergeMaker
{
      public static void main(String[] args) {
            int[] a = { 1, 2, 3 };
            int[] b = { 7, 8, 9 };
            Merger m = new Merger(a, b);
            int[] c = m.getArray();
            for (int x=0; x < c.length; x++) {
                  System.out.println(c[x]);
            }
      }
}

ellandrd
0
 
objectsCommented:
you need to concatenate the two arrays

http://www.objects.com.au/java/qa/1839197685.html
0
 
ellandrdCommented:
thank you

ellandrd
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mnrzCommented:
what you mean "new array" if you want the returned array be separated from the original you may clone the returned array:

for(...){
      Object[] myNewArray = returenedArray.clone();
}

or use System.arraycopy() to copy to new array object

0
 
ellandrdCommented:
What it not good enough for a grade A?

ellandrd
0
 
ScottRogersGCAuthor Commented:
can't go round handing out grade A's every day :-)
0
 
ellandrdCommented:
Your a Premium Service Member like me - isn't points unlimited? so who cares?

;-)
0
 
mnrzCommented:
ellandrd,
your solution is not efficient

the objects' post is fundamental way of resolving the problem so he is eligible to achieve the points
0
 
CEHJCommented:
System.arraycopy
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now