0

I have a method that is required to return an array of longs.

long[] mainMethod() {

     //create the resultant array 
     long[] result = null;

     method1(result);   // Their job is to append new long values to the array
     method2(result);
}

I was hoping to do something like this:

    // Update the result array
    int origLen = (result == null) ? 0 : result.length;
    long[] newResult = new long[origLen + 4];

    if (origLen != 0) {
        newResult = Arrays.copyOf(result, origLen + 4);
    }

    newResult[origLen + 0] = someLong;
    newResult[origLen + 1] = someLong;
    newResult[origLen + 2] = someLong;
    newResult[origLen + 3] = someLong;
    result = newResult;

when I realized java passes references by value so I can't change the reference here. If cant't change the definitions of these methods (the result to be generate is to be passed as argument as some other return value exists), how can I update the original array?? I've been told NOT to use ArrayList ( I could update the methods to take ArrayList, but I was told it's silly to use ArrayList and eventually return array of longs)..

I was thinking I could initially allocate 4 long values and then keep passing it around and copying it over like:

    result = Arrays.copyOf(result, origLen + 4);

I think this could work, but then how can I check if the array returned by actual, mainMethod contains some useful info? As of now, I was checking it to be null..

Thanks in advance.

user1071840
  • 3,522
  • 9
  • 48
  • 74

2 Answers2

0

You can do this:

long[] mainMethod() {

     //create the resultant array 
     long[] result = null;

     result = method1(result);   // Their job is to append new long values to the array
     result = method2(result);
}

Just make sure that your method1 and method 2 return long[].

James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • I already have some info being returned from these methods, so I can't return the array. – user1071840 Sep 11 '13 at 14:06
  • That is a bummer. Though I'm wondering why, if you already have info returning from method1 and method2, are you not assigning any variables to them? You might as well refactor these methods to return result if you aren't using the info. – James Dunn Sep 11 '13 at 14:24
0

If you want a dynamically resizable data structure, don't use an array.

In this case, you could very easily use a LinkedList.

List<Long> result = new LinkedList<>();
method(result); // adds to result
method(result); // adds more to result

Long[] array = result.toArray(new Long[result.size()]);
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • What's the difference in using an ArrayList and LinkedList? Our only requirement solved by either of these is the growable part? – user1071840 Sep 11 '13 at 14:32
  • An `ArrayList` internally uses an array. So when it has to dynamically grow it has to first copy its elements to a new, bigger array. This can hurt performance in extreme cases. Read here: http://stackoverflow.com/questions/11667955/difference-between-arraylist-and-linkedlist – Sotirios Delimanolis Sep 11 '13 at 14:33