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 );
Answer from tangens on Stack OverflowThe 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 );
Use a List<String>, such as an ArrayList<String>. It's dynamically growable, unlike arrays (see: Effective Java 2nd Edition, Item 25: Prefer lists to arrays).
import java.util.*;
//....
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
System.out.println(list); // prints "[1, 2, 3]"
If you insist on using arrays, you can use java.util.Arrays.copyOf to allocate a bigger array to accomodate the additional element. This is really not the best solution, though.
static <T> T[] append(T[] arr, T element) {
final int N = arr.length;
arr = Arrays.copyOf(arr, N + 1);
arr[N] = element;
return arr;
}
String[] arr = { "1", "2", "3" };
System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3]"
arr = append(arr, "4");
System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3, 4]"
This is O(N) per append. ArrayList, on the other hand, has O(1) amortized cost per operation.
See also
- Java Tutorials/Arrays
- An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
- Java Tutorials/The List interface
Videos
Hi all, so I have an empty array I just created like this:
int[] array = new int[5];
How do I add numbers to it? I tried array.append() but it's not working. I don't want to do it manually like array[0] etc I want to just keep adding to the tail.
EDIT: I'm sure I won't go over the limit of what the array can contain. I just want to know how to add to the tail of the array without having to specify what position is being added.
You cannot add a method to "the array class", same for all predefined classes, the only solution for other predefined classes is to inherit them and add the methods you want, but, this cannot be done for arrays as it has not a specific predefined class, it can be defined as a container object that holds a fixed number of values of a single type (Arrays).
The alternative solutions for what you want:
Use Lists (
ArrayListfor example), as described in the comments.concatenate the array original content with the new element, for this solution, you can use ArrayUtils.addAll(T[] array1, T... array2) or System.arraycopy like the following sample (convert the new element to an array before)
String[] both = ArrayUtils.addAll(first, second);Create a class to wrap the array (adding the array as a variable in this class), and create addElement method containing something like the following:
int[] newArray = new int[length + 1]; for (int i = 0; i < length; i++) newArray[i] = this.array[i]; newArray[length] = element; length ++; array = newArray;
Is there a way to implement the method as non-static (in a "predefined array class" or something? I'm not too sure how to express it better) in such a way that it would work as follows instead?
No, there is not. You must use something like List, or optionally write your own interface that wraps an array (it cannot itself be an array).