开发者

Interleave - in java doesn't properly work

开发者 https://www.devze.com 2023-03-17 07:49 出处:网络
I implement this interleave method in java but it doesn\'t work properly. Where is my error? I would like to mix 2 String List.

I implement this interleave method in java but it doesn't work properly. Where is my error? I would like to mix 2 String List.

["a","b","c"]
["1","2","3","4"]

result should be = [a, 1, b, 2, c, 3, 4] but I got only [a, 1, b, 2, c, 3]开发者_StackOverflow社区.

public static List<String> interleave(List<String> list1,List<String>list2){
    List<String> result= new ArrayList<String>();

    for(int i=0; i<list1.size(); i++){
        result.add(list1.get(i));
        result.add(list2.get(i));
    }
    return result;
}

many thanks in advance.

regards, koko


The error is that you're only looking at list1.size() whereas in your case the second list is longer than the first.

You should quite possibly use the iterators instead:

Iterator<String> iterator1 = list1.iterator();
Iterator<String> iterator2 = list2.iterator();

while (iterator1.hasNext() || iterator2.hasNext())
{
    if (iterator1.hasNext())
    {
        result.add(iterator1.next());
    }
    if (iterator2.hasNext())
    {
        result.add(iterator2.next());
    }
}

You could just use the sizes, finding the minimum one and then filling in the rest afterwards, but the code's likely to get ugly. Also, the above code is generalizable to any two Iterable<String> values, not just List<String>.


Because you are looping over list1 size which is 3. You can do something like this:

public static List<String> interleave(List<String> list1,List<String>list2){
        List<String> result= new ArrayList<String>();

        if(list1.size()<list2.size()){
            for(int i=0; i<list1.size(); i++){
                result.add(list1.get(i));
                result.add(list2.get(i));
            }
            for (int i = list1.size(); i < list2.size(); i++) {
                result.add(list2.get(i));
            }
        } else {
            for(int i=0; i<list2.size(); i++){
                result.add(list1.get(i));
                result.add(list2.get(i));
            }
            for (int i = list2.size(); i < list1.size(); i++) {
                result.add(list1.get(i));
            }
        }
        return result;
    }


Actually you do not handle the case where both lists have different sizes. That's the error.

With your current code

  1. It will ignore some values if size(list2) > size(list1)
  2. It will throw an exception if size(list1) > size(list2)

This should work (or at least give an idea, wasn't able to test)

public static List<String> interleave(List<String> list1,List<String>list2){
    List<String> result= new ArrayList<String>();

    List<String> minList = list1.size() <= list2.size() ? list1 : list2;
    List<String> maxList = list1.size() <= list2.size() ? list2 : list1;

    // interleave up to the size of the smaller list
    for(int i=0; i<minList.size(); i++){
        result.add(list1.get(i));
        result.add(list2.get(i));
    }

    // add the other elements from the bigger list
    for(int i=minList.size(); i < maxList.size(); i++) {
        result.add(maxList.get(i));
    }

    return result;
}


Well, what about having a look at the size of your 1st list and then thinking about how often the for loop gets executed?


Your loop is running only until it reaches the size of list1. This may cause two problems:

  1. list2 is longer (as in your case), so if list1 size is X, you'll add X elements from list1 and X elements from list2.

  2. Even worse - list2 is shorter - in this case you'll get an exception, because you are running out of the list boundaries.

You should perform this loop Y times, when Y is the size of the shorter list, and then add the rest of the elements of the longer list.


public static List<String> interleave(List<String> list1,List<String>list2){
    List<String> result= new ArrayList<String>();

    int common = Math.min(list1.size(),list2.size());
    for(int i=0; i<common; i++){
        result.add(list1.get(i));
        result.add(list2.get(i));
    }
    for(int i=common; i<list1.size(); i++){
        result.add(list1.get(i));
    }
    for(int i=common; i<list2.size(); i++){
        result.add(list2.get(i));
    }
    return result;
}

Or:

public static List<String> interleave(List<String> list1,List<String>list2){
    List<String> result= new ArrayList<String>();

    int max = Math.max(list1.size(),list2.size());
    for(int i=0; i<max; i++){
        if (i < list1.size()) {
            result.add(list1.get(i));
        }
        if (i < list2.size()) {
            result.add(list2.get(i));
        }
    }
    return result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号