2

I have this bugging problem I cannot figure out. I have this exercise:

Complete the following generic Pair class, so that execution of the program gives the indicated output. Do not change main.

class Pair ... {   ...  }

 class GenericPairTest {        
 public static void main(String[] args) {       
    Pair<String,Integer> phoneNumber = new Pair<>("Bill's number", 1324);   
        System.out.println(phoneNumber);

           Pair<Double,Double> point = new Pair<>(3.14, 2.32);      
       System.out.println(point);   
     } 
 }

The output is suposed to be like that:

Bill's number 1324

3.14 2.32`

I tried doing this:

import java.util.*;

class Pair <T,U> {

    ArrayList<T,U> newList = new ArrayList<>();

  Pair(T inT, U inU){
    newList.add(inT,inU);
  }

}

class GenericPairTest {

    public static void main(String[] args) {        
        Pair<String,Integer> phoneNumber = 
            new Pair<>("Bill's number", 1324);  
        System.out.println(phoneNumber);
        Pair<Double,Double> point = 
            new Pair<>(3.14, 2.32);
        System.out.println(point);
    }
}

But it doesn't work :(

1
  • 2
    When you say "doesn't work :(" - What do you mean? Compilation error? Logical error? This will help people helping you better. Commented Dec 18, 2013 at 11:37

5 Answers 5

10

Why do you need a list? You just need to store the two objects:

class Pair <T,U> {
    private T _t;
    private U _u;

    public Pair(T t, U u) {
        _t = t;
        _u = u;

    public String toString() {
        return _t + " " + _u;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Then why is he creating an arraylist in the main? :( Isn't the syntax "class<type> name = new class<>();" used to create lists?
Who creates arraylist? Where?
I get confused by this bit Pair<String,Integer> phoneNumber = new Pair<>("Bill's number", 1324);
Ohhhhhhh! It's just stating that we create a new Pair class and pass in as T a String and as U an Integer, call this instance "phoneNymber" and use new to create a reference, and put Bill's number as String and 1324 as Integer :D isn't it?
5

You don't need the ArrayList in your Pair class.

Just keep the two values as two generic members.

Comments

4

You are looking for this I think:

ArrayList<Pair<T,U>>

but I think you should create an internal Pair class which might look something like this:

public class Pair<T, U> {
    private T left;
    private U right;
    // getters/setters + constructor for them
}

You can use a Map as well like the others pointed out but you don't need an ArrayList.

Comments

3

You can't do that with ArrayList, but since your pair always holds exactly two objects, it really doesn't need to store them in a list. Just use two fields:

class Pair<T,U> {
    final T t;
    final U u;

    Pair(T t, U u) {
        this.t = t;
        this.u = u;
    }

    @Override
    public String toString() {
        return t + " " + u;
    }
}

The way you're using the ArrayList there is very suggestive of a Map. For example, you could write:

Map<T,U> map = new HashMap<>();
Pair(T t, U u) {
    map.put(t, u);
}

But since a map can hold any number of key-value pairs, it is not really the right structure to use for storing a single pair.

Comments

1

If your case, it would be enough to remember the pairs in the Pair class, and not have an internal ArrayList for that.

After that, externally, you can include Pairs in list.

final List<Pair<T,U>> pairList = new ArrayList<>();

Also, sorry to say this, but you're missing some very basic OOP skills here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.