0

I need to split an array into thirds, and the only effective way I can think of is using the copyOfRange method. Is there a way for me to return all three arrays because I can't seem to do so? If not, what is the best way to divide one array three ways?

public char[] board(char[] a) {
    char[] b = Arrays.copyOfRange(a, 0, a.length / 3);
    char[] c = Arrays.copyOfRange(a, a.length / 3, a.length - 3);
    char[] d = Arrays.copyOfRange(a, a.length - 3, a.length);

    return null;
}
Gia
  • 45
  • 1
  • 6
  • 2
    How about returning a `char[][]`? – Sweeper Feb 16 '21 at 03:44
  • @Sweeper, the first thing I thought of. But the assignment wants me to return just a single dimension array. Java won't allow me to return a two-dimensional array in a single dimension array method. – Gia Feb 16 '21 at 03:48
  • 3
    Then the assignment is unclear, or you haven't understood what you are supposed to do. Please ask your instructor. – Sweeper Feb 16 '21 at 03:50
  • Are you returning those three arrays, or using them internally? Perhaps you can associate a length and an offset with the array for each of your thirds, and use the data in situ. – tgdavies Feb 16 '21 at 03:59

0 Answers0