There are a few considerations depending on whether the original was a collection.
If you had an array Data[] data,
String[] dataCopy = new String[data.length];
int i = 0;
for (Data datum: data){
if (datum==null)break;
dataCopy[i++] = datum;
}
But that is not optimal because you would be assigning more array cells than necessary if the original data had 100 cells but the 50th cell is where the empty string is found.
Using an ArrayList would let the JVM manage the expansion of cells, so that at the end of it you just convert the ArrayList to an array using toArray(). It's not a conversion really, but toArray withdraws the internally managed array from the ArrayList.
ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data){
if (datum==null)break;
dataList.add(datum);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
Or if the array you are processing is a member of data:
ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data.getItems()){
String name = datum.getName();
if (name==null)break;
dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
Or if the original data structure implements Iterable. Let's say the class of items is Item.
Iterator<Item> itemit = data.getItems().iterator();
ArrayList<String> dataList = new ArrayList<String>(data.length);
while(itemit.hasNext()){
String name = itemit.next().getName;
if (name==null)break;
dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);