Cut the arrays from position pos. - Java Collection Framework

Java examples for Collection Framework:Array Sub Array

Description

Cut the arrays from position pos.

Demo Code

/*/*from   ww  w  . j a v  a2 s .  co  m*/
 This file is part of p300.


 p300 is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 p300 is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with p300.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int pos = 2;
        System.out.println(java.util.Arrays.toString(cutAt(b, pos)));
    }

    /**
     * Cut the arrays from position pos.
     * Won't change the original array.
     * @param b
     * @param pos
     * @return Copy of the array, only between pos and end
     */
    public static byte[] cutAt(byte[] b, int pos) {
        if (b == null || pos <= 0) {
            return b;
        }
        if (pos >= b.length) {
            return new byte[0];
        }
        byte[] rest = new byte[b.length - pos];
        for (int i = 0; i < b.length - pos; i++) {
            rest[i] = b[i + pos];
        }

        return rest;
    }
}

Related Tutorials