开发者

Java List "zipping"

开发者 https://www.devze.com 2023-03-22 17:59 出处:网络
I have the cla开发者_如何学JAVAss Pair: public class Pair<T1, T2> { T1 info1; T2 info2; Pair<T1,T2> next;

I have the cla开发者_如何学JAVAss Pair:

public class Pair<T1, T2> {

    T1 info1;
    T2 info2;
    Pair<T1,T2> next;
    Pair<T1,T2> prev;

    Pair(T1 info1,T2 info2,Pair<T1,T2> next, Pair<T1,T2> prev) {
        this.info1 = info1;
        this.info2 = info2;
        this.next = next;
        this.prev = prev;
    }

    @Override
    public String toString() {
        return info1.toString() + info2.toString();
    }
}

I must implement a class Zipper, that has two static methods: zip and unzip.

zip is 'zipping' two lists into one and unzipp from one in two.

For example when I have the two lists:

list1: 1 , 2 ,3 ,4
list2: a, b, c, d 

zip must return : 1a, 2b, 3c, 4d and unzip must reverse that.

I have no idea how to do that, can someone help me out?


  1. don't implement your own LinkedList - this work is already done (iteration is done, etc.)
  2. make Pair only 2-tuple So then you can implement method like this (be careful I wrote it here, in browser - so there may be typos):

Zipper:


public class Zipper {

    static <T1, T2> List<Pair<T1, T2>> zip(List<T1> input1, List<T2> input2){
        int inputSize = input1.size();
        if(inputSize != input2.size()){
            throw  new IllegalArgumentException("Different input sizes.");
        }
        List<Pair<T1,T2>> output = new LinkedList<Pair<T1,T2>>();
        for (int i = 0; i < inputSize; ++i){
           output.add(new Pair<T1,T2>(input1.get(i), input2.get(i)));
        }
        return output;
    }

    static <T1,T2> Pair<List<T1>, List<T2>> unzip(List<Pair<T1, T2>> input){
        List<T1> output1 = new LinkedList<T1>();
        List<T2> output2 = new LinkedList<T2>();
        for(Pair<T1, T2> pair : input){
            output1.add(pair.getInfo1());
            output2.add(pair.getInfo2());
        }
        return new Pair<List<T1>, List<T2>>(output1, output2);
    }

    public static void main(String[] args) {
        List<Integer> input1 = Arrays.asList(1,2,3,4);
        List<Character> input2 = Arrays.asList('a','b','c','d');
        List<Pair<Integer, Character>> zipped = zip(input1, input2);
        System.out.println("zipped: " + zipped);
        Pair<List<Integer>,List<Character>> output = unzip(zipped);
        System.out.println("unzipped1: " + output.getInfo1());
        System.out.println("unzipped2: " + output.getInfo2());
    }
}

Pair:


public class Pair<T1, T2> {
    T1 info1;
    T2 info2;

    public Pair(T1 info1, T2 info2) {
        this.info1 = info1;
        this.info2 = info2;
    }

    public T1 getInfo1() {
        return info1;
    }

    public T2 getInfo2() {
        return info2;
    }

    @Override
    public String toString() {
        return info1.toString() + info2.toString();
    }
}


It's a bit unclear from your question, but I think that your example would be more usefully written (where [] means list and Pair means an object constructed by "new Pair(x, y)"):

Input: 
 list1: [1, 2, 3, 4]
 list2: [a, b, c, d]
Output:
   zip: [Pair<1, a>, Pair<2, b>, Pair<3, c>, Pair<4, d>]

That is, you end you end up with a list of pairs.

What's not clear to me is whether (as delnan points out) that you have to create a linked list or not. What are the signatures of zip and unzip you've been asked to write?

0

精彩评论

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