如何将新的元素添加到一个数组中? [英] How to add new elements to an array?

查看:189
本文介绍了如何将新的元素添加到一个数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");
where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");

这两个附加不进行编译。如何将这项工作是否正确?

Those two appends are not compiling. How would that work correctly?

推荐答案

数组的大小不能修改。如果你想要一个更大的数组,你必须实例化一个新的。

The size of an array can't be modified. If you want a bigger array you have to instantiate a new one.

有一个更好的解决办法是使用 的ArrayList 它,你需要它可以成长。该方法 ArrayList.toArray(T []一)给你回你的数组,如果你需要它以这种形式。

A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a ) gives you back your array if you need it in this form.

List<String> where = new ArrayList<String>();
where.add( ContactsContract.Contacts.HAS_PHONE_NUMBER+"=1" );
where.add( ContactsContract.Contacts.IN_VISIBLE_GROUP+"=1" );

如果您需要将其转换为一个简单的数组...

If you need to convert it to a simple array...

String[] simpleArray = new String[ where.size() ];
where.toArray( simpleArray );

但大多数事情你做一个数组你可以用这个ArrayList的事,太:

But most things you do with an array you can do with this ArrayList, too:

// iterate over the array
for( String oneItem : where ) {
    ...
}

// get specific items
where.get( 1 );

这篇关于如何将新的元素添加到一个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆