Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the code I’ve to perform appending. But it’s not working.

String[] where;

where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");

where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");

Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

You can’t modify the size of an array. So you need to instantiate a new array. You have to use an ArrayList which extends as per the real-time requirements. 

Check the below code which uses ArrayList.toArray( T[] a ): 

List<String> where = new ArrayList<String>();

where.add( ContactsContract.Contacts.HAS_PHONE_NUMBER+"=1" );

where.add( ContactsContract.Contacts.IN_VISIBLE_GROUP+"=1" );

To convert it to a simple array, you can do this:

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

where.toArray( simpleArray );

Or else you can do it like below:

// iterate over the array

for( String oneItem : where ) {

    ...

}

// get specific items

where.get( 1 );

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
asked Aug 29, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...