Java ME没有大小限制的字符串数组

3
我有一个简单的问题?
String[] names  = null ;

names[0] = "Hello"

我遇到了一个错误...

由于我不知道数组的大小限制,该如何实例化数组...请帮助我。

9个回答

4
当你事先不知道数组大小时,请使用ArrayList<String>。在这里,您正在执行无效操作(尝试访问空对象)。

编辑:由于您不能使用Vector和ArrayList,您将不得不自己实现动态数组。您可以在algolist.net上找到一个几乎准备好了的动态数组实现,并带有一些解释。只需将int存储更改为String存储即可。

// 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);
}

    //...
}

1
@Muhammad:j2me拥有Vector,你可以使用它替代ArrayList:https://dev59.com/questions/cE_Sa4cB1Zd3GeqP_1uD - Thilo
@Thilo:他在另一条评论中说他由于某些原因无法使用Vector。 - Lukasz

3

尝试

String[] names = new String[1];
names[0] = "Hello";

如果您事先不知道大小,请使用ArrayList<String>

ArrayList<String> names = new ArrayList<String>();
names.add("hello");
names.add("another string");
...

看起来j2me没有泛型的ArrayList,你可以像下面这样使用它。

ArrayList names = new ArrayList();
names.add("hello");
names.add("another string");
....

String name = (String) names.get(1);

零大小的数组?使用此代码片段将导致ArrayIndexOutOfBoundsException :) - Code Painters
@Muhammad:确实,这就是为什么其他人建议使用动态大小的数据结构。 - Code Painters

3
这个怎么样?
String[] names  = new String[] {  "Hello" };

或者你也可以使用 ArrayListStringCollection

编辑:

对于 J2ME:这里有一个用于动态整数数组的技巧here。我想应该可以将其转换为字符串。我已经转换了那个例子,但是我没有 J2ME 模拟器来测试它:

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--;
        }
    }
}

1
String str[] = { "ABCde","xyZ","sdsdsdf"} ;

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

1

如果你不知道大小限制(或更一般的情况),你几乎总是会想使用一个 List 而不是一个数组,因为它更容易处理。

List<String> names = new ArrayList<String>();
names.add("Hello");

你之所以会收到一个异常(NullPointerException),是因为你只定义了一个变量来引用一个String数组,但没有创建String数组。
你需要像这样初始化它:
String[] names = new String[10];

1

你可以与向量一起使用

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);

就像这样...

希望这有帮助。

我永远不会使用这个!!但还是谢谢。 - Makky
将集合转换为数组最好使用以下方式:strings.toArray(new String[strings.size()]) - ddimitrov

1
抱歉,这是不可能的。在Java中,数组的大小是固定的,您必须在创建数组时指定大小。
如果您需要一个灵活的缓冲区,请考虑使用ArrayList。它会根据需要增长。

1

既然你在使用 J2ME,且无法使用 arraylist,我认为你别无选择。

你需要选择一个合理的数组起始大小,注意数组大小,如果需要添加的对象超过了数组大小,就将它复制到一个更大的数组中。

在我们的限制下,我想不出其他办法。


1

老兄,我看了一下那个......你知道它的J2ME JAR文件在哪里吗?那是Javolution。 - Makky
你好!你知道在哪里可以获取支持ArrayList和HashMap的Javolution的Jar文件吗? - Makky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接