j2me中的动态数组

时间:2009-08-24 14:13:36

标签: arrays java-me

我想在我的j2me应用程序中执行一个简单的动态数组,

我看到的唯一动态数组是“java.util.Vector”,而且这个数组似乎不接受int作为新元素(只想要对象)。

那么我该如何解决这个问题?

2 个答案:

答案 0 :(得分:6)

您需要在整数中填充int。

v.addElement(new Integer(1));

答案 1 :(得分:5)

public class DynamicIntArray
{
    private static final int CAPACITY_INCREMENT = 10;
    private static final int INITIAL_CAPACITY   = 10;

    private final int capacityIncrement;

    public int   length = 0;
    public int[] array;

    public DynamicIntArray(int initialCapacity, int capacityIncrement)
    {
        this.capacityIncrement = capacityIncrement;
        this.array = new int[initialCapacity];
    }

    public DynamicIntArray()
    {
        this(CAPACITY_INCREMENT, INITIAL_CAPACITY);
    }

    public int append(int i)
    {
        final int offset = length;
        if (offset == array.length)
        {
            int[] old = array;
            array = new int[offset + capacityIncrement];
            System.arraycopy(old, 0, array, 0, offset);
        }
        array[length++] = i;
        return offset;
    }


    public void removeElementAt(int offset)
    {
        if (offset >= length)
        {
            throw new ArrayIndexOutOfBoundsException("offset too big");
        }

        if (offset < length)
        {
            System.arraycopy(array, offset+1, array, offset, length-offset-1);
            length--;
        }
    }
}

没有setAt()方法,但我相信你明白了。