Essentially I have the task of writing two methods: a zip method that turns a generic tuple containing two lists of generic objections into a list of tuples made up of elements of the aforementioned lists, and an unzip method that does the same thing in the other direction. I.e.:
({1, "banana", true}{asgard, 5.21e, "stupidhomework"})
=> unzip
{(1, asgard)("banana",5.21e)(true,"stupidhomework")}
=> zip
({1, "banana", true}{asgard, 5.21e, "stupidhomework"})
Further complicating this: the list is a simply linked immutable generic list with the structure
List<T>
public final T head;
public final List<T> tail;
…And the tuples are equally generic; both elements of the tuple can be literally anything:
public class Tuple<A, B> {
public final A a;
public final B b;
So here’s my problem: I have no idea how to get at elements a second layer down. For example, normally, I’d just do this-
public static<A, B extends List> List unzip(Tuple<A, B> ListenTuple){
List TupelnListe = new List(new Tuple(ListenTuple.a.head, ListenTuple.b.head));
-To get at the elements I need, two layers down. Unfortuantely, because Tuple is generic, it doesn’t know that ListenTuple.a has to be a List, and therefore has a head. The analogous problem arises in zip with getting at a and b from the tuple. Oh, and I’m not allowed to typecast for some stupid arbitrary reason, although I’m not sure how that would help me.
…Basically, can anyone give me a pointer on how to get this started? They basically threw us into generics in class this week, and nobody gets it. Any assistance would be much appreciated.
Would this work?
So that A and B are the elements of the list and a, b are now lists.
I am more familiar with C++ so am not sure if the differences prevent this somehow.