0

this is what i have so far

int[] startInts = Arrays.copyOfRange(myInts, count+1, myInts.length); // first half of array
int[] endInts = Arrays.copyOfRange(myInts, count+1, myInts.length); // other half of array

int[] newInts = new int[startInts.length + endInts.length];
System.arraycopy(startInts, 0, newInts, 0, startInts.length);
System.arraycopy(endInts, 0, newInts, startInts.length, endInts.length);

myInts = newInts;

But all this does is give me a number.

It doesn't really merge the two arrays into one array. Any help with how to do this.

Solution

int[] startInts = Arrays.copyOfRange(myInts, myInts[0], index); // first half of array
        int[] endInts = Arrays.copyOfRange(myInts, count+1, myInts.length); // other half of array

        System.out.println(Arrays.toString(startInts));
        System.out.println(Arrays.toString(endInts));
        
        int[] newInts = new int[startInts.length + endInts.length];
        System.arraycopy(startInts, 0, newInts, 0, startInts.length);
        System.arraycopy(endInts, 0, newInts, startInts.length, endInts.length);
Community
  • 1
  • 1
user3439273
  • 125
  • 1
  • 2
  • 12

4 Answers4

1

Class System

Method

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Look javadoc for all set of functions


EDIT

int[] startInts = Arrays.copyOfRange(myInts, count+1, myInts.length); // first half of array
int[] endInts = Arrays.copyOfRange(myInts, count+1, myInts.length); // other half of array

The possible issue that you face here is that the startInts and endInts are the same, so its not the first half and second half, its just first half and first Half.

Dileep
  • 5,362
  • 3
  • 22
  • 38
  • if you see the code i did this – user3439273 Mar 20 '14 at 05:31
  • the problem is with the `startInts` and `endInts` both are the same.so you get an array with double the size of startInts like `[endInts,endInts]` – Dileep Mar 20 '14 at 05:36
  • you are right i fixed it, i mustve pressed ctrl+z and didnt notice, but i still dont get the desired output – user3439273 Mar 20 '14 at 05:40
  • print both the array and check what's the array value for `startInts` and `endInts` – Dileep Mar 20 '14 at 05:41
  • Wow it worked, i cant believe it was just a simple typo causing all the wrong outputs. Thank you so much for pointing that out i really was not even paying attention to that. Solution is on top – user3439273 Mar 20 '14 at 05:45
0

You should use :

String[] both = ArrayUtils.addAll(first, second);

when ArrayUtils is a class from Apache Commons Lang library

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
binhdv83
  • 21
  • 2
0

why not use ArrayList's

ArrayList<String> a = new ArrayList<String>();
ArrayList<String> b = new ArrayList<String>();

a.add("a1");
a.add("a2");

b.add("b1");
b.add("b2");

ArrayList<String> c = new ArrayList<String>();
c.addAll(a);
c.addAll(b);

System.out.println(c.toString());

Of course if you want to start out with real arrays then you can convert them into ArrayList using Arrays.asList(array)

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-1
String[] both = (String[]) ArrayUtils.addAll(first, second);

or

static String[] concat(String[] a, String[] b) {
   String[] c= new String[a.length+b.length];
   System.arraycopy(a, 0, c, 0, a.length);
   System.arraycopy(b, 0, c, a.length, b.length);
   return c;
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Lilacs
  • 1