48

I have an array of size 300000 and i want it to split it into 2 equal parts. Is there any method that can be used here to achieve this goal?

Will it be faster than the for-loop operation or it will cause no effect on performance?

5 Answers 5

66

You can use System.arraycopy().

int[] source = new int[1000];

int[] part1 = new int[500];
int[] part2 = new int[500];

//              (src   , src-offset  , dest , offset, count)
System.arraycopy(source, 0           , part1, 0     , part1.length);
System.arraycopy(source, part1.length, part2, 0     , part2.length);
2
  • 3
    Using Arrays.copyOf() and Arrays.copyOfRange() might be slightly faster (but it will definitely be shorter code ;-)). Commented Apr 20, 2011 at 13:35
  • 9
    @Joachim Arrays.copyOf() and Arrays.copyOfRange() are also just wrappers around System.arrayCopy(). They just have a prettier call syntax and do some basic validation, that's all Commented Apr 20, 2011 at 15:00
59

copyOfRange

This does what you want without you having to create a new array as it returns a new array.

int[] original = new int[300000];
int[] firstHalf = Arrays.copyOfRange(original, 0, original.length/2);
2
  • 1
    @Thomas Thanks, fixed. I always assume people know to add the proper namespaces, references, etc which is a bad habit I need to break.
    – Pete
    Commented Apr 20, 2011 at 13:41
  • Note that the 'to' param is exclusive (see doc). int[] secondHalf = Arrays.copyOfRange(original, original.length/2, original.length);
    – torno
    Commented Jun 19, 2017 at 9:20
9

Splits an array in multiple arrays with a fixed maximum size.

public static <T extends Object> List<T[]> splitArray(T[] array, int max){

  int x = array.length / max;
  int r = (array.length % max); // remainder

  int lower = 0;
  int upper = 0;

  List<T[]> list = new ArrayList<T[]>();

  int i=0;

  for(i=0; i<x; i++){

    upper += max;

    list.add(Arrays.copyOfRange(array, lower, upper));

    lower = upper;
  }

  if(r > 0){

    list.add(Arrays.copyOfRange(array, lower, (lower + r)));

  }

  return list;
}

Example - an Array of 11 shall be splitted into multiple Arrays not exceeding a size of 5:

// create and populate an array
Integer[] arr = new Integer[11];

for(int i=0; i<arr.length; i++){
  arr[i] = i;
}

// split into pieces with a max. size of 5
List<Integer[]> list = ArrayUtil.splitArray(arr, 5);

// check
for(int i=0; i<list.size(); i++){

  System.out.println("Array " + i);

  for(int j=0; j<list.get(i).length; j++){
    System.out.println("  " + list.get(i)[j]);
  }
}

Output:

Array 0
  0
  1
  2
  3
  4
Array 1
  5
  6
  7
  8
  9
Array 2
  10
2
  • Your Logic wont work man :( Integer[] arr = new Integer[11]; for 11 elements : only 2 Arrays :P
    – Amit Singh
    Commented Jul 4, 2013 at 15:56
  • @Amit Singh Thanks: I forgot the remainder. Corrected the code. Unlike some other answers, for this solution you do not need to know the dimension of the result.
    – diggi
    Commented Jun 20, 2015 at 1:36
2

Use System.arraycopy.

4
  • 1
    a) This is a Link to Java 1.4 (1.6 has been current for 4+ years) b) an example would be nice Commented Apr 20, 2011 at 13:34
  • @Sean: I often just fix a), because there is not good reason to link those ancient texts ;-) Commented Apr 20, 2011 at 13:37
  • @Sean: I hope for a day when googling for "Java String" will point to the current version of the docs as the first result ;-) Commented Apr 20, 2011 at 13:40
  • 1
    @Joachim that's why I (and probably all Java power users here) always google java <classname> /6 Commented Apr 20, 2011 at 13:42
0

Use this code it works perfectly for odd or even list sizes. Hope it help somebody .

 int listSize = listOfArtist.size();
 int mid = 0;
 if (listSize % 2 == 0) {
    mid = listSize / 2;
    Log.e("Parting", "You entered an even number. mid " + mid
                    + " size is " + listSize);
 } else {
    mid = (listSize + 1) / 2;
    Log.e("Parting", "You entered an odd number. mid " + mid
                    + " size is " + listSize);
 }
 //sublist returns List convert it into arraylist * very important
 leftArray = new ArrayList<ArtistModel>(listOfArtist.subList(0, mid));
 rightArray = new ArrayList<ArtistModel>(listOfArtist.subList(mid,
                listSize));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.