2011-04-27 70 views
3

我有一個簡單的問題?Java ME字符串數組無大小

String[] names = null ; 

names[0] = "Hello" 

我得到一個錯誤..

我怎麼能實例化陣列,因爲我不知道的大小限制...幫我

回答

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

cant ..on j2me ...需要使用數組...甚至不應該使用矢量... – Makky 2011-04-27 12:34:53

+0

@Muhammad:請參閱我的編輯。 – Lukasz 2011-04-27 12:41:57

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

,但如果我需要在索引1上添加另一個,那麼它會給一個錯誤的傢伙... – Makky 2011-04-27 12:33:08

+0

零大小的數組?你會得到ArrayIndexOutOfBoundsException異常在這個片段:) – 2011-04-27 12:34:01

+0

@Muhammad:的確,這就是爲什麼其他人提出了一個動態大小的數據結構。 – 2011-04-27 12:34:54

4

使用ArrayList<String>當您事先不知道數組大小。你在這裏做的是無效的(試圖訪問一個空對象)。


編輯:因爲你不能使用Vector和ArrayList,你必須推出自己的,你執行動態數組。你會在algolist.net上找到幾乎準備好的解釋。只需用String存儲替換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); 
} 

    //... 
} 
+1

cant ... on j2me – Makky 2011-04-27 12:33:54

+1

@Muhammad:j2me有Vector,你可以用它代替:http://stackoverflow.com/questions/3343212/arraylist-in-j2me – Thilo 2011-04-27 12:38:39

+0

@Thilo:他在其他評論中說他可以由於某種原因,不使用Vector。 – Lukasz 2011-04-27 12:42:39

1

我怎麼能實例化陣列,因爲我不知道的大小限制

對不起,這是不能做完了。數組的大小固定爲Java,並且在創建數組時必須給出大小。

如果您需要靈活的緩衝區,請考慮使用ArrayList。這將根據需要增長。

1

如果您不知道大小限制(或者更一般地說:幾乎總是),那麼您將需要使用List而不是數組,因爲處理起來要舒服得多。

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

你得到一個異常的原因(一NullPointerException)是,你只定義一個變量來引用一個String - 陣列,但沒有創建String陣列。

你必須初始化它是這樣的:

String[] names = new String[10]; 
1

當你在J2ME,說你不能使用ArrayList我沒有看到你有任何選擇。

你需要選擇你的陣列合理的起始大小,看大小,如果你需要比大小,以增加更多的對象,將其複製到一個更大的陣列。

隨着我們的限制,我想不出的另一種方法。

1

正如你解釋喲想用它在J2ME沒有提供J2ME的ArrayList但是這裏存在一個實現:

http://kickjava.com/src/j2me/util/ArrayList.java.htm

,您可以嘗試。

你也應該考慮在這裏太:

http://www1.j2mepolish.org/javadoc/j2me/de/enough/polish/util/ArrayList.html

+0

傢伙我看對...都知道它的J2ME jar文件..thats javolution。 – Makky 2011-04-27 13:14:12

+0

實施前的重要通知:http://forum.enough.de/viewtopic.php?t=1308&sid=72721678bd8adb1c189da4f0f48ea00f和jar的鏈接:http://www.enough.de/products/j2me-polish/download/download/ – kamaci 2011-04-27 13:29:17

+0

Ta Kamaci !!杜知道從哪裏得到JAR文件的Javolution一個......以支持ArrayList和包含HashMap是嗎? – Makky 2011-04-27 14:54:05

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

使用這樣的.. 希望這種有益

+0

我永遠不會使用這個!但無論如何, – Makky 2011-04-27 14:55:20

+0

將集合轉換爲數組最好像這樣完成:strings.toArray(new String [strings.size()]) – ddimitrov 2011-07-17 15:22:12

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

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