I have the following code:
String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + \"=1\");
where.append(ContactsContract.Contacts.IN_VISIB
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);
}
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));
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.
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);
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!!
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]);
}
}