Copy an array of signed values in int, into an array of short. The value is truncated as necessary. - Java java.lang

Java examples for java.lang:int Array

Description

Copy an array of signed values in int, into an array of short. The value is truncated as necessary.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] src = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays
                .toString(copySignedIntToShortArray(src)));
    }/* ww  w .ja  v a 2s. c o m*/

    /**
     * <p>Copy an array of signed values in int, into an array of short.</p>
     *
     * <p>The value is truncated as necessary.</p>
     *
     * @param   src   an array of int
     * @return      an array of short
     */
    static public short[] copySignedIntToShortArray(int[] src) {
        if (src == null)
            return null;
        int n = src.length;
        short[] dst = new short[n];
        for (int j = 0; j < n; ++j) {
            dst[j] = (short) (src[j]); // truncates
        }
        return dst;
    }
}

Related Tutorials