无大小的Java ME字符串数组

无大小的Java ME字符串数组,java,arrays,java-me,size,Java,Arrays,Java Me,Size,我有一个简单的问题 String[] names = null ; names[0] = "Hello" 我有个错误 我怎么能实例化数组,因为我不知道大小限制。。。帮帮我试试 String[] names = new String[1]; names[0] = "Hello"; 如果你事先不知道尺寸,就用一个 ArrayList<String> names = new ArrayList<String>(); names.add("hello"); names.a

我有一个简单的问题

String[] names  = null ;

names[0] = "Hello"
我有个错误

我怎么能实例化数组,因为我不知道大小限制。。。帮帮我试试

String[] names = new String[1];
names[0] = "Hello";
如果你事先不知道尺寸,就用一个

ArrayList<String> names = new ArrayList<String>();
names.add("hello");
names.add("another string");
...
如果事先不知道数组大小,请使用
ArrayList
。您在此处执行的操作无效(尝试访问空对象)


编辑:因为不能使用Vector和ArrayList,所以必须滚动自己的动态数组实现。你会发现一个几乎准备好了,上面有一些解释。只需将int存储替换为字符串存储即可

// Warning: not tested!
public class DynamicStringArray {
    private String[] storage;
    private int size;

    public DynamicArray() {
            storage = new String[10];
            size = 0;
    }

    public DynamicArray(int capacity) {
            storage = new String[capacity];
            size = 0;
    }

public void ensureCapacity(int minCapacity) {
    int capacity = storage.length;
    if (minCapacity > capacity) {
        int newCapacity = (capacity * 3) / 2 + 1;
        if (newCapacity < minCapacity)
            newCapacity = minCapacity;
        storage = Arrays.copyOf(storage, newCapacity);
    }
}

private void pack() {
    int capacity = storage.length;
    if (size <= capacity / 2) {
        int newCapacity = (size * 3) / 2 + 1;
        storage = Arrays.copyOf(storage, newCapacity);
    }
}

public void trim() {
    int newCapacity = size;
    storage = Arrays.copyOf(storage, newCapacity);
}

    //...
}
//警告:未测试!
公共类DynamicStringArray{
私有字符串[]存储;
私有整数大小;
公共图书馆{
存储=新字符串[10];
尺寸=0;
}
公共交通走廊(国际通行能力){
存储=新字符串[容量];
尺寸=0;
}
公共空间容量(国际最小容量){
int容量=存储长度;
如果(最小容量>容量){
int newCapacity=(容量*3)/2+1;
if(新容量<最小容量)
新容量=最小容量;
存储=阵列.copyOf(存储,新容量);
}
}
专用空包(){
int容量=存储长度;
如果(尺寸这个怎么样

String[] names  = new String[] {  "Hello" };
或者也可以使用
ArrayList
StringCollection

编辑:

对于J2ME:针对int的动态数组发布了一个技巧。我想应该可以将其转换为字符串。我已经转换了该示例,但是我没有J2ME emulator来测试它:

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

    private final int capacityIncrement;

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

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

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

    public int append(String str) {
        final int offset = length;
        if (offset == array.length) {
            String[] old = array;
            array = new String[offset + capacityIncrement];
            System.arraycopy(old, 0, array, 0, offset);
        }
        array[length++] = str;
        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--;
        }
    }
}
公共类DynamicStringArray{
专用静态最终整容增量=10;
专用静态最终int初始容量=10;
私人最终int能力增加;
公共整数长度=0;
公共字符串[]数组;
公共DynamicStringArray(int initialCapacity,int capacityIncrement){
this.capacityIncrement=电容增量;
this.array=新字符串[初始容量];
}
公共DynamicStringArray(){
这(容量增量、初始容量);
}
公共int追加(字符串str){
最终整数偏移=长度;
if(偏移量==数组.length){
字符串[]old=数组;
数组=新字符串[偏移量+电容增量];
System.arraycopy(旧的,0,数组,0,偏移量);
}
数组[length++]=str;
返回偏移量;
}
公共void removeElementAt(内部偏移){
如果(偏移量>=长度){
抛出新的ArrayIndexOutOfBoundsException(“偏移量太大”);
}
如果(偏移量<长度){
System.arraycopy(数组,偏移量+1,数组,偏移量,长度-偏移量
- 1);
长度--;
}
}
}
我不知道数组的大小限制,如何实例化数组

很抱歉,这无法完成。数组在Java中是固定大小的,您必须在创建数组时给出大小


如果需要一个灵活的缓冲区,请考虑使用一个ARAYLIST,这将根据需要增长。

< P>如果你不知道大小限制(或者更一般地说:几乎总是),你会想要使用一个<代码>列表>代码,而不是一个数组,因为它更舒服地处理。
List<String> names = new ArrayList<String>();
names.add("Hello");

当你在J2ME上说你不能使用arraylist时,我看你别无选择

您需要为数组选择一个合理的起始大小,观察大小,如果需要添加比大小更多的对象,请将其复制到更大的数组中


由于我们的限制,我想不出其他方法。

正如您所解释的,您希望在J2ME上使用它,没有为J2ME提供ArrayList,但是这里有一个实现:

你可以试试

你也应该考虑一下:


您可以与vector一起使用

Vector strings=new Vector();
strings.addElement("HELLO");
//then convert it to string array
String str[]=new String[strings.size()];
str[0]=(String)strings.get(0);
像这样。。
希望这是一个有用的

零大小数组?您将通过以下代码片段获得ArrayIndexOutOfBoundsException:)@Muhammad:的确,这就是为什么其他人建议使用动态大小的数据结构。@Muhammad:j2me有Vector,您可以使用它:@Thilo:他在其他评论中说,出于某种原因,他不能使用Vector。老兄,我已经看过了…你知道它的j2me jar文件在哪里..这是一个解决方案。实现之前的一个重要注意事项:和j的链接ar:Ta Kamaci!!你知道从哪里获取Javolution one的Jar文件吗…它支持Arraylist和hashmaps,嗯?我永远不会使用这个!!但是Ta无论如何将集合转换为数组最好是这样做:strings.toArray(新字符串[strings.size()]))
String str[] = { "ABCde","xyZ","sdsdsdf"} ;

String str[][] = { {"ABC","abc"} ,
                {"pqr","qwerTY"}
              } ;
Vector strings=new Vector();
strings.addElement("HELLO");
//then convert it to string array
String str[]=new String[strings.size()];
str[0]=(String)strings.get(0);
String str[] = { "ABCde","xyZ","sdsdsdf"} ;

String str[][] = { {"ABC","abc"} ,
                {"pqr","qwerTY"}
              } ;