How to add new elements to an array?

后端 未结 18 1692
误落风尘
误落风尘 2020-11-22 04:55

I have the following code:

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


        
相关标签:
18条回答
  • 2020-11-22 05:08

    you can create a arraylist, and use Collection.addAll() to convert the string array to your arraylist

    0 讨论(0)
  • 2020-11-22 05:15

    As tangens said, the size of an array is fixed. But you have to instantiate it first, else it will be only a null reference.

    String[] where = new String[10];
    

    This array can contain only 10 elements. So you can append a value only 10 times. In your code you're accessing a null reference. That's why it doesnt work. In order to have a dynamically growing collection, use the ArrayList.

    0 讨论(0)
  • 2020-11-22 05:16

    If one really want to resize an array you could do something like this:

    String[] arr = {"a", "b", "c"};
    System.out.println(Arrays.toString(arr)); 
    // Output is: [a, b, c]
    
    arr = Arrays.copyOf(arr, 10); // new size will be 10 elements
    arr[3] = "d";
    arr[4] = "e";
    arr[5] = "f";
    
    System.out.println(Arrays.toString(arr));
    // Output is: [a, b, c, d, e, f, null, null, null, null]
    
    0 讨论(0)
  • 2020-11-22 05:17

    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 );
    
    0 讨论(0)
  • 2020-11-22 05:17

    Size of array cannot be modified. If you have to use an array, you can use:

    System.arraycopy(src, srcpos, dest, destpos, length); 
    
    0 讨论(0)
  • 2020-11-22 05:18

    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
    0 讨论(0)
提交回复
热议问题