Skip to content
Fix Code Error

How to add new elements to an array?

March 13, 2021 by Code Error
Posted By: Anonymous

I have the following 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?

Solution

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

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 );

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 );
Answered By: Anonymous

Related Articles

  • Use of Jquery on scroll event
  • Adding Dynamic Input Fields With VueJs
  • android get all contacts
  • How to get the Android device's primary e-mail address
  • How do you clear the SQL Server transaction log?
  • laravel vuejs/axios put request Formdata is empty
  • How to prevent Vue from loading the data multiple times?
  • How to prevent scrolling the whole page?
  • Form field border-radius is not working only on the…
  • When to use LinkedList over ArrayList in Java?
  • Xamarin Forms: Android Phone contacts is not listing
  • Adding a UserCreationForm to html in Django
  • Vuejs Property or method is not defined on the…
  • Collection in Backbone.js Not Firing Events
  • How to solve Internal Server Error in Next.Js?
  • How to filter a RecyclerView with a SearchView
  • How to convert number to words in java
  • `white-space: nowrap` inside flex container only…
  • Show/hide 'div' using JavaScript
  • Why is Ember throwing "Uncaught Error: Assertion…
  • Text size and different android screen sizes
  • How to use java.net.URLConnection to fire and handle…
  • Start redis-server with config file
  • Logging best practices
  • Backbone.js - Is this button and event bound correctly?
  • Difference between
  • Get value (String) of ArrayList(); in Java
  • collision detection from picturebox in a list c#
  • Using Auto Layout in UITableView for dynamic cell…
  • What is the worst programming language you ever worked with?
  • Why the value of input file missing when I input the…
  • Instantiate blocks evenly between two instantiated objects
  • Dart Polymer error 'List' is not a subtype of type…
  • How to use Servlets and Ajax?
  • What is an IndexOutOfRangeException /…
  • How to recover stashed uncommitted changes
  • Smart way to truncate long strings
  • Convert list to array in Java
  • How to call Android contacts list?
  • What does a "Cannot find symbol" or "Cannot resolve…
  • make arrayList.toArray() return more specific types
  • Centos/Linux setting logrotate to maximum file size…
  • You have not concluded your merge (MERGE_HEAD exists)
  • Unresponsive hamburger icon in HTML
  • Change column type in pandas
  • How to submit Polymer forms to PHP and display response
  • Memcached vs. Redis?
  • Flexbox not working correctly with Aurelia (Webpack…
  • How can I pass modelformset_factory validation in Django?
  • Backbone.js app not rendering view
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • commandButton/commandLink/ajax action/listener…
  • Efficiency of Java "Double Brace Initialization"?
  • The definitive guide to form-based website authentication
  • Knight's tour Problem - storing the valid moves then…
  • How can I get a property projection filtered by same…
  • How does PHP 'foreach' actually work?
  • For-each over an array in JavaScript
  • Using multiprocessing on Image processing
  • What is Cache-Control: private?
  • Django - update inline formset not updating
  • How to find out client ID of component for ajax…
  • Understanding PrimeFaces process/update and JSF…
  • git status shows modifications, git checkout --…
  • How flex: 100% works in css?
  • What does "Fatal error: Unexpectedly found nil while…
  • Usage of __slots__?
  • Why is there no SortedList in Java?
  • How to pass an ArrayList to a varargs method parameter?
  • How to dynamically add and remove form fields in Angular 2
  • Argument 1 passed to…
  • Triggering text area by clicking related select/checkbox
  • Best way to convert an ArrayList to a string
  • How can I access and process nested objects, arrays or JSON?
  • Convert ArrayList to String array in Android
  • Checkout another branch when there are uncommitted…
  • Server side load a global component with Next.js in React
  • Content showing over the top of tooltip
  • How to dispatch a Redux action with a timeout?
  • VueJS how to rerender a component
  • UnsatisfiedDependencyException: Error creating bean…
  • Ember.js - CRUD scenarios - Specifying View from…
  • How can I manually compile a svelte component down…
  • Vue.js vuetify v-textarea not auto growing correctly…
  • Trouble with qsort with key/value structs in a btree
  • How to paste yanked text into the Vim command line
  • modal View controllers - how to display and dismiss
  • Sorting an ArrayList of objects using a custom sorting order
  • Why does C++ code for testing the Collatz conjecture…
  • How to deserialize Arraylist items into concrete…
  • Why do I have to "git push --set-upstream origin "?
  • Algo not working for String Decode Ways -- ,…
  • How to declare an ArrayList with values?
  • What is a NullReferenceException, and how do I fix it?
  • What are type hints in Python 3.5?
  • Getting started with Haskell
  • Ways to save Backbone.js model data?
  • Add a custom attribute to a Laravel / Eloquent model…
  • What is your most productive shortcut with Vim?
  • Is it possible to apply CSS to half of a character?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I remedy “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?

Next Post:

How do I kill the process currently using a port on localhost in Windows?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error