How to add new elements to an array?

后端 未结 18 1693
误落风尘
误落风尘 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:27
    String[] source = new String[] { "a", "b", "c", "d" };
    String[] destination = new String[source.length + 2];
    destination[0] = "/bin/sh";
    destination[1] = "-c";
    System.arraycopy(source, 0, destination, 2, source.length);
    
    for (String parts : destination) {
      System.out.println(parts);
    }
    
    0 讨论(0)
  • 2020-11-22 05:28

    There are many ways to add an element to an array. You can use a temp List to manage the element and then convert it back to Array or you can use the java.util.Arrays.copyOf and combine it with generics for better results.

    This example will show you how:

    public static <T> T[] append2Array(T[] elements, T element)
    {
        T[] newArray = Arrays.copyOf(elements, elements.length + 1);
        newArray[elements.length] = element;
    
        return newArray;
    }
    

    To use this method you just need to call it like this:

    String[] numbers = new String[]{"one", "two", "three"};
    System.out.println(Arrays.toString(numbers));
    numbers = append2Array(numbers, "four");
    System.out.println(Arrays.toString(numbers));
    

    If you want to merge two array you can modify the previous method like this:

    public static <T> T[] append2Array(T[] elements, T[] newElements)
    {
        T[] newArray = Arrays.copyOf(elements, elements.length + newElements.length);
        System.arraycopy(newElements, 0, newArray, elements.length, newElements.length);
    
        return newArray;
    }
    

    Now you can call the method like this:

    String[] numbers = new String[]{"one", "two", "three"};
    String[] moreNumbers = new String[]{"four", "five", "six"};
    System.out.println(Arrays.toString(numbers));
    numbers = append2Array(numbers, moreNumbers);
    System.out.println(Arrays.toString(numbers));
    

    As I mentioned, you also may use List objects. However, it will require a little hack to cast it safe like this:

    public static <T> T[] append2Array(Class<T[]> clazz, List<T> elements, T element)
    {
        elements.add(element);
        return clazz.cast(elements.toArray());
    }
    

    Now you can call the method like this:

    String[] numbers = new String[]{"one", "two", "three"};
    System.out.println(Arrays.toString(numbers));
    numbers = append2Array(String[].class, Arrays.asList(numbers), "four");
    System.out.println(Arrays.toString(numbers));
    
    0 讨论(0)
  • 2020-11-22 05:30

    If you would like to store your data in simple array like this

    String[] where = new String[10];
    

    and you want to add some elements to it like numbers please us StringBuilder which is much more efficient than concatenating string.

    StringBuilder phoneNumber = new StringBuilder();
    phoneNumber.append("1");
    phoneNumber.append("2");
    where[0] = phoneNumber.toString();
    

    This is much better method to build your string and store it into your 'where' array.

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

    Adding new items to String array.

    String[] myArray = new String[] {"x", "y"};
    
    // Convert array to list
    List<String> listFromArray = Arrays.asList(myArray);
    
    // Create new list, because, List to Array always returns a fixed-size list backed by the specified array.
    List<String> tempList = new ArrayList<String>(listFromArray);
    tempList.add("z");
    
    //Convert list back to array
    String[] tempArray = new String[tempList.size()];
    myArray = tempList.toArray(tempArray);
    
    0 讨论(0)
  • 2020-11-22 05:33

    I've made this code! It works like a charm!

    public String[] AddToStringArray(String[] oldArray, String newString)
    {
        String[] newArray = Arrays.copyOf(oldArray, oldArray.length+1);
        newArray[oldArray.length] = newString;
        return newArray;
    }
    

    I hope you like it!!

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

    It's also possible to pre-allocate large enough memory size. Here is a simple stack implementation: the program is supposed to output 3 and 5.

    class Stk {
        static public final int STKSIZ = 256;
        public int[] info = new int[STKSIZ];
        public int sp = 0; // stack pointer
        public void push(int value) {
            info[sp++] = value;
        }
    }
    class App {
        public static void main(String[] args) {
            Stk stk = new Stk();
            stk.push(3);
            stk.push(5);
            System.out.println(stk.info[0]);
            System.out.println(stk.info[1]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题