How do I add a new element to an array?

Asked 2 years ago, Updated 2 years ago, 114 views

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

I tried this way to add elements to the String array, but it couldn't be compiled. What should I fix?

string java array

2022-09-22 22:16

1 Answers

You cannot change the size of an array. If you want to contain more elements, you need to create a new array. Another option is to write ArrayList.

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

You can use it like this. And if you want to re-arrange this into a string array,

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

You can do it like this. But most of the things you can do with an array, you can do with an ArrayList.


2022-09-22 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.