I totally get what Priyanka is saying, and that’s a solid approach when the size is known. But let’s say you don’t know the size up front, or you absolutely need to stick with arrays for some reason. In that case, you can manually resize the array by using Arrays.copyOf()
. It’s a way to simulate the idea of java append to array, but with a bit of manual effort:
String[] where = new String[0]; // Start with an empty array
where = Arrays.copyOf(where, where.length + 1);
where[where.length - 1] = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
where = Arrays.copyOf(where, where.length + 1);
where[where.length - 1] = ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1";
This way, you manually resize the array each time you want to add an element. While it technically works, it’s not as efficient as using ArrayList, and it’s more code than necessary. If you’re doing a lot of “appending” operations, I’d definitely recommend considering ArrayList for its flexibility and ease of use. But this method will get the job done if you prefer sticking with arrays!