Reputation: 1659
What would be the easiest / quickest way to zip a list of strings which is similar to this:
john,barry,stewart,josh,30,45,23,56
I want the order to be
john,30,barry,45,stewart,23,josh,56
I know that sounds like homework but the actual list is used in some selenium code and stores urls and the option of a radio button on the page but I thought the above example is simpler to read.
Upvotes: 1
Views: 249
Reputation: 4779
It seems as if you are just trying to interleave the numbers and the alphanumerics. I would just scan the list in order to find the index of the first number than create a new list and take the first alpha and the first number and add them to the new list, then the second alpha and second number. This is not sorting and so is O(n)
Upvotes: 0
Reputation: 17945
Generic version of zip
:
@SafeVarargs
public static <T> ArrayList<T> zip(Iterable<T> ...iterables) {
ArrayList<Iterator<T>> is = new ArrayList<>(iterables.length);
for (int i=0; i<iterables.length; i++) {
is.add(iterables[i].iterator());
}
ArrayList<T> al = new ArrayList<>();
while (is.get(0).hasNext()) {
for (int i=0; i<is.size(); i++) {
// FIXME: could check for shorter-than-expected sublists here
al.add(is.get(i).next());
}
}
return al;
}
Can be tested / called as follows:
public static void main(String[] args) {
List<String> parts =
Arrays.asList("john,barry,stewart,josh,30,45,23,56".split(","));
List<String> names = parts.subList(0, parts.size()/2);
List<String> numbers = parts.subList(parts.size()/2, parts.size());
System.err.println(zip(names, numbers));
}
Upvotes: 0
Reputation: 2445
Try this way : 1. First sort it - the numbers will cover half the list and names half the list. 2. Create two arrays half the length : sArr1 and sArr2. Store numbers in sArr1 and names in sArr2. 3. Merge them by putting alternatively the number and the name from the two arrays into original array.
import java.util.Arrays;
public class SortMix {
public static void main(String[] args) {
String sArr[] = {"john","barry","stewart","josh","30","45","23","56"};
Arrays.sort(sArr);
display(sArr);
String sArr1[] = new String[sArr.length/2];
for(int i=0;i<sArr.length/2;i++)
sArr1[i] = sArr[i];
display(sArr1);
String sArr2[] = new String[sArr.length/2];
for(int i=0;i<sArr.length/2;i++)
sArr2[i] = sArr[i+sArr.length/2];
display(sArr2);
int k=0;
int l=0;
for(int i=0;i<sArr.length;i++) {
if(i%2==0){
sArr[i]=sArr1[k];
k++;
} else {
sArr[i]=sArr2[l];
l++;
}
}
display(sArr);
}
public static void display(String[] sArr) {
for(int i=0;i<sArr.length;i++)
System.out.print(sArr[i] + " ");
System.out.println();
}
}
Output would be :
23 30 45 56 barry john josh stewart
23 30 45 56
barry john josh stewart
23 barry 30 john 45 josh 56 stewart
Upvotes: 0
Reputation: 36349
Your question sounds like you need code to fix a bad design. Therefore, I would go a step back and ask the question how the list
john,barry,stewart,josh,30,45,23,56
was created, when you obviously need:
(john, 30), (barry, 45), (stewart, 23), (josh, 56)
(Replace the (x,y)
notation with you favorite tuple type.)
Upvotes: 1
Reputation: 8171
I would do something similar to this:
String[] parts = input.split(",");
int halfLength = parts.length / 2;
for (int i = 0; i < halfLength; i++) {
String name = parts[i];
String age = parts[i + halfLength];
// Store them in whatever structure you want here
}
Upvotes: 6
Reputation: 723
Looks like two lists
First generate two lists and create a new result list
Pseudocode :
list = Arrays.AsList(string.split("\\s+"))
list1 = list.sublist(0, list.size() /2 );
list2 = list.sublist(list.size() /2 +1 , list.size());
for(int i = 0;i<list1.size() ; i++ ) {
resultlist.add(list1.get(i);
resultlist.add(list2.get(i);
}
return resultlist
Upvotes: 0
Reputation: 30548
What you need here is a zip
function like in functional languages.
Functional java may help you out.
Related: Is there an accepted Java equivalent to Python's zip()?
Upvotes: 2
Reputation: 2102
Here you need to do some manual code.
Firstly scatter all the string and integer separately and obtain two list.
then just iterate one list and add these two list in one final list like first string and
then integer.
Upvotes: 0