I have data in the following format:
["DATA1-1","DATA1-2","DATA1-3","DATA1-4","","DATA2-1","DATA2-2","DATA2-3","DATA2-4","","DATA3-1","DATA3-2","DATA3-3","DATA3-4",""]
I would like to split this array into several arrays, where the delimiter should be an empty item (""). Something like this:
[["DATA1-1","DATA1-2","DATA1-3","DATA1-4"],["DATA2-1","DATA2-2","DATA2-3","DATA2-4"],["DATA3-1","DATA3-2","DATA3-3","DATA3-4"]].
This is the code I came up with:
private List<List<String>> retrieveData(List<String> arrayIn)
{
List<List<String>> subArrays = new ArrayList<>();
List<String> tempArrays = new ArrayList<>();
for(int i=0; i<arrayIn.size(); i++)
{
if(!airwayIn.get(i).equals("") && i != (airwayIn.size()-1) )
{
tempArrays.add(airwayIn.get(i));
}
else if (airwayIn.get(i).equals("") || i == (airwayIn.size()-1) )
{
subArrays.add(tempArrays);
tempArrays = new ArrayList<>();
}
}
return subArrays;
}
But I was wondering whether there is a more elegant code to do it. For example, this is what I use in Swift:
let subArrays: [[String]] = airwayIn.split(separator: "").map{Array($0)}
Thank you!