Convert int a Short


I have the code:

int x:

ArrayList < Short> lista;

How do I convert x to Short to be able to add it to the dynamic array list by using the statement list.add

asked by Jairo Tovar 14.02.2017 в 01:11

1 answer


You can perform a cast of the variable, that is, before saving it, change the data type:

(short) x; 

Make the change from x to int .

import java.util.ArrayList;

public class Aplicacion {

    public static void main(String[] args) {

        int x = 30;
        int y = 40;

        ArrayList<Short> lista = new ArrayList<Short>();

        lista.add((short) x);
        lista.add((short) y);


answered by 14.02.2017 в 01:16