Example usage for java.lang.reflect Array setInt

List of usage examples for java.lang.reflect Array setInt

Introduction

In this page you can find the example usage for java.lang.reflect Array setInt.

Prototype

public static native void setInt(Object array, int index, int i)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified int value.

Usage

From source file:Main.java

public static void main(String args[]) {
    int[] array = new int[] { 1, 2, 3 };

    Array.setInt(array, 1, 100);

    System.out.println(Arrays.toString(array));

}

From source file:MainClass.java

public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);

    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        int value = i;
        Array.setInt(array, i, value);
    }/*from  w w w. j a  v a2s .  c o m*/

    for (int i : (int[]) array) {
        System.out.println(i);

    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);

    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }/*from w  w w.  j av a2  s.  com*/

    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }
}

From source file:ArrayTroubleAgain.java

public static void main(String... args) {
    Integer[] ary = new Integer[2];
    try {/*from   www .j a  va2s  .com*/
        Array.setInt(ary, 0, 1); // IllegalArgumentException

        // production code should handle these exceptions more gracefully
    } catch (IllegalArgumentException x) {
        err.format("Unable to box%n");
    } catch (ArrayIndexOutOfBoundsException x) {
        x.printStackTrace();
    }
}

From source file:CreateMatrix.java

public static void main(String... args) {
    Object matrix = Array.newInstance(int.class, 2, 2);
    Object row0 = Array.get(matrix, 0);
    Object row1 = Array.get(matrix, 1);

    Array.setInt(row0, 0, 1);
    Array.setInt(row0, 1, 2);//w  ww  . j a  v  a 2  s  . co m
    Array.setInt(row1, 0, 3);
    Array.setInt(row1, 1, 4);

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            out.format("matrix[%d][%d] = %d%n", i, j, ((int[][]) matrix)[i][j]);
}

From source file:MainClass.java

private static void fillArray(Object array) {
    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }/*from   w w  w .ja va  2s  .  com*/
}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object array, Class type, int index, JSONArray jsonArray)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        if (type == Integer.TYPE) {
            Array.setInt(array, index, jsonArray.getInt(index));
        } else if (type == Long.TYPE) {
            Array.setLong(array, index, jsonArray.getInt(index));
        } else if (type == Short.TYPE) {
            Array.setShort(array, index, (short) jsonArray.getInt(index));
        } else if (type == Boolean.TYPE) {
            Array.setBoolean(array, index, jsonArray.getBoolean(index));
        } else if (type == Double.TYPE) {
            Array.setDouble(array, index, jsonArray.getDouble(index));
        } else if (type == Float.TYPE) {
            Array.setFloat(array, index, (float) jsonArray.getDouble(index));
        } else if (type == Character.TYPE) {
            char ch = jsonArray.getString(index).charAt(0);
            Array.setChar(array, index, ch);
        } else if (type == Byte.TYPE) {
            Array.setByte(array, index, (byte) jsonArray.getInt(index));
        } else {//from w w w . j  a v a 2 s.c o m
            throw new JSONSerializationException("Unknown primitive: " + type);
        }
    } else if (type == String.class) {
        Array.set(array, index, jsonArray.getString(index));
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONObject jObj = jsonArray.getJSONObject(index);
        JSONSerializable serObj = deSerialize(type, jObj);
        Array.set(array, index, serObj);
    } else if (type.isArray()) {
        Class componentClass = type.getComponentType();
        JSONArray subArray = jsonArray.getJSONArray(index);
        int len = subArray.length();

        Object newArray = Array.newInstance(componentClass, len);
        for (int k = 0; k < len; ++k) {
            loadObject(newArray, componentClass, k, subArray);
        }
    }
}

From source file:org.kv.KVConfig.java

/**
 * Code to read a json file into this structure.
 * Sorta possible with the json library, but this works
 *
 * @param configFile File to read./*  w w w .  j ava  2  s .c  o m*/
 * @return A KVConfig instance with values set from the file.
 */
public static KVConfig load(File configFile) {
    try {
        BufferedReader in = new BufferedReader(new FileReader(configFile));
        KVConfig config = new KVConfig();

        // read the whole file into a single string
        String jsonStr = "";
        String line;
        while ((line = in.readLine()) != null)
            jsonStr += line + "\n";
        in.close();

        // build a json object from the string
        JSONObject json = new JSONObject(jsonStr);

        // while there are keys, set the members of the KVConfig instance
        Iterator<?> keyiter = json.keys();
        while (keyiter.hasNext()) {
            String key = (String) keyiter.next();
            Field field = KVConfig.class.getField(key);

            // handle arrays first, then scalars
            if (field.getType().isArray()) {
                JSONArray array = json.getJSONArray(key);
                Class<?> component = field.getType().getComponentType();

                Object value = Array.newInstance(component, array.length());
                for (int i = 0; i < array.length(); i++) {
                    if (component == int.class) {
                        Array.setInt(value, i, array.getInt(i));
                    }
                    if (component == long.class) {
                        Array.setLong(value, i, array.getLong(i));
                    } else if (component == String.class) {
                        Array.set(value, i, array.getString(i));
                    } else if (component == double.class) {
                        Array.setDouble(value, i, array.getDouble(i));
                    }
                }

                field.set(config, value);
            } else if (field.getType() == int.class) {
                field.setInt(config, json.getInt(key));
            } else if (field.getType() == long.class) {
                field.setLong(config, json.getLong(key));
            } else if (field.getType() == String.class) {
                field.set(config, json.getString(key));
            } else if (field.getType() == double.class) {
                field.set(config, json.getDouble(key));
            }
        }

        return config;

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }

    return null;
}

From source file:com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.java

protected Object convertArray(FacesContext facesContext, UISelectMany uiSelectMany, Class componentType,
        String[] newSubmittedValues) throws ConverterException {

    // component type of String means no conversion is necessary
    if (componentType.equals(String.class)) {
        return newSubmittedValues;
    }//from   www .  j  a  v  a2  s  .co  m

    // if newSubmittedValue is null return zero-length array
    if (newSubmittedValues == null) {
        return Array.newInstance(componentType, 0);
    }

    // create the array with specified component length
    int numberOfValues = newSubmittedValues.length;
    Object convertedValues = Array.newInstance(componentType, numberOfValues);

    // Determine if a converter is explicitly registered with the component
    Converter converter = uiSelectMany.getConverter();
    if (converter == null) {
        // Determine if there is a default converter for the class
        converter = getConverterForClass(componentType);
    }
    if (converter == null) {
        // we don't need to convert base Object types
        if (componentType.equals(Object.class)) {
            return newSubmittedValues;
        } else {
            throw new ConverterException("Converter is null");
        }
    }

    for (int index = 0; index < numberOfValues; index++) {

        // convert the next element
        Object nextConvertedElement = converter.getAsObject(facesContext, uiSelectMany,
                newSubmittedValues[index]);

        if (!componentType.isPrimitive()) {
            Array.set(convertedValues, index, nextConvertedElement);
        } else if (componentType.equals(Boolean.TYPE)) {

            Array.setBoolean(convertedValues, index, ((Boolean) nextConvertedElement).booleanValue());

        } else if (componentType.equals(Integer.TYPE)) {

            Array.setInt(convertedValues, index, ((Integer) nextConvertedElement).intValue());

        } else if (componentType.equals(Long.TYPE)) {

            Array.setLong(convertedValues, index, ((Long) nextConvertedElement).longValue());

        } else if (componentType.equals(Short.TYPE)) {

            Array.setShort(convertedValues, index, ((Short) nextConvertedElement).shortValue());

        } else if (componentType.equals(Byte.TYPE)) {

            Array.setByte(convertedValues, index, ((Byte) nextConvertedElement).byteValue());

        } else if (componentType.equals(Float.TYPE)) {

            Array.setFloat(convertedValues, index, ((Float) nextConvertedElement).floatValue());

        } else if (componentType.equals(Double.TYPE)) {

            Array.setDouble(convertedValues, index, ((Double) nextConvertedElement).doubleValue());

        } else if (componentType.equals(Character.TYPE)) {

            Array.setChar(convertedValues, index, ((Character) nextConvertedElement).charValue());

        }
    }
    return convertedValues;
}

From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java

protected Object handleArrayCase(FacesContext context, UISelectMany uiSelectMany, Class arrayClass,
        String[] newValues) throws ConverterException {
    Object result = null;//from  w  ww  .  j a va  2 s.c  om
    Class elementType = null;
    Converter converter = null;
    int i = 0, len = (null != newValues ? newValues.length : 0);

    elementType = arrayClass.getComponentType();

    // Optimization: If the elementType is String, we don't need
    // conversion.  Just return newValues.
    if (elementType.equals(String.class)) {
        return newValues;
    }

    try {
        result = Array.newInstance(elementType, len);
    } catch (Exception e) {
        throw new ConverterException(e);
    }

    // bail out now if we have no new values, returning our
    // oh-so-useful zero-length array.
    if (null == newValues) {
        return result;
    }

    // obtain a converter.

    // attached converter takes priority
    if (null == (converter = uiSelectMany.getConverter())) {
        // Otherwise, look for a by-type converter
        if (null == (converter = Util.getConverterForClass(elementType, context))) {
            // if that fails, and the attached values are of Object type,
            // we don't need conversion.
            if (elementType.equals(Object.class)) {
                return newValues;
            }
            String valueStr = "";
            for (i = 0; i < newValues.length; i++) {
                valueStr = valueStr + " " + newValues[i];
            }
            Object[] params = { valueStr, "null Converter" };

            throw new ConverterException(Util.getExceptionMessage(Util.CONVERSION_ERROR_MESSAGE_ID, params));
        }
    }

    Util.doAssert(null != result);
    if (elementType.isPrimitive()) {
        for (i = 0; i < len; i++) {
            if (elementType.equals(Boolean.TYPE)) {
                Array.setBoolean(result, i,
                        ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i])).booleanValue());
            } else if (elementType.equals(Byte.TYPE)) {
                Array.setByte(result, i,
                        ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i])).byteValue());
            } else if (elementType.equals(Double.TYPE)) {
                Array.setDouble(result, i,
                        ((Double) converter.getAsObject(context, uiSelectMany, newValues[i])).doubleValue());
            } else if (elementType.equals(Float.TYPE)) {
                Array.setFloat(result, i,
                        ((Float) converter.getAsObject(context, uiSelectMany, newValues[i])).floatValue());
            } else if (elementType.equals(Integer.TYPE)) {
                Array.setInt(result, i,
                        ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i])).intValue());
            } else if (elementType.equals(Character.TYPE)) {
                Array.setChar(result, i,
                        ((Character) converter.getAsObject(context, uiSelectMany, newValues[i])).charValue());
            } else if (elementType.equals(Short.TYPE)) {
                Array.setShort(result, i,
                        ((Short) converter.getAsObject(context, uiSelectMany, newValues[i])).shortValue());
            } else if (elementType.equals(Long.TYPE)) {
                Array.setLong(result, i,
                        ((Long) converter.getAsObject(context, uiSelectMany, newValues[i])).longValue());
            }
        }
    } else {
        for (i = 0; i < len; i++) {
            if (log.isDebugEnabled()) {
                Object converted = converter.getAsObject(context, uiSelectMany, newValues[i]);
                log.debug("String value: " + newValues[i] + " converts to : " + converted.toString());
            }
            Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i]));
        }
    }
    return result;
}