在J2ME int的动态数组 [英] Dynamic Array of ints in j2me
本文介绍了在J2ME int的动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的整数一个简单的动态数组在我的J2ME应用程序,
唯一的动态数组我看到的是java.util.Vector中,这一次似乎并没有接受一个int作为新元素(只希望对象)。
那我怎么到处去修复这个问题?
解决方案
公共类DynamicIntArray
{
私有静态最终诠释CAPACITY_INCREMENT = 10;
私有静态最终诠释INITIAL_CAPACITY = 10; 私人最终诠释将按capacityIncrement; 公众诠释长度= 0;
公众诠释[]数组; 公共DynamicIntArray(INT参数:initialCapacity,诠释将按capacityIncrement)
{
this.capacityIncrement =将按capacityIncrement;
this.array =新INT [参数:initialCapacity];
} 公共DynamicIntArray()
{
这个(CAPACITY_INCREMENT,INITIAL_CAPACITY);
} 公众诠释追加(int i)以
{
最终诠释偏移量=长度;
如果(偏移== array.length)
{
INT [] =旧阵列;
阵列=新的INT [偏移+将按capacityIncrement];
System.arraycopy(老,0阵列,0,偏移);
}
数组[长++] =我;
返回偏移;
}
公共无效removeElementAt(INT偏移)
{
如果(偏移> =长度)
{
抛出新的ArrayIndexOutOfBoundsException异常(偏移太大);
} 如果(偏移<长度)
{
System.arraycopy(数组,偏移+ 1,阵列,胶印,长度偏移-1);
长度 - ;
}
}
}
没有一个setAt()方法,但我敢肯定你的想法。
I want to do a simple dynamic array of ints in my j2me application,
The only dynamic array I see is "java.util.Vector" and this one doesn't seem to accept an int as a new element (only wants Objects).
So how do I go around fixing that problem?
解决方案
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--;
}
}
}
Doesn't have a setAt() method, but I'm sure you get the idea.
这篇关于在J2ME int的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文