2011-04-27 71 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"} 
       } ;