1

I have class Pager. And I need to put in lists in various types and I need to returns the same list types.

Code of Pager There is only constructor and getData method, that only get a subset and returns it.

public class Pager<E> {
private int page;
private int amount;
private List<E> list;

/**
 * Constructor
 * @param amount 
 * @param page 
 * @param list 
 */
public Pager(int page, int amount, List<E> list) {
    this.page = page;
    this.amount = amount;
    this.list = list;
}

/**
 * Returns data to counter
 * @return
 */
public List<E> getData() {
    List<E> result = new ArrayList<E>();

    int from = this.page * this.amount;
    int to = (this.page * this.amount) + this.amount;

    for (int i = from; i < to; i++) {
        result.add(this.list.get(i));
    }

    return result;
}

Method call I call pager with lists and then I need to put results back to the lists.

List<MyType1> list1 = ArrayList<Mytype1>();
List<MyType2> list2 = ArrayList<Mytype2>();
Pager pager = new Pager(
                    page,
                    amount,
                    list1;
                  );
list1 = pager.getData();


Pager pager = new Pager(
                    page,
                    amount,
                    list2
                  );

list2 = pager.getData();

So how can I make this pager generic to process various types of list?

Thanks for the help.

5
  • Did you forget to add your questions? Commented Jan 9, 2013 at 8:49
  • 4
    so what was the question? Commented Jan 9, 2013 at 8:49
  • 1
    also you might want to use method "subList" to get a sublist. Commented Jan 9, 2013 at 8:53
  • and you are using Pager without type parameter try Pager<MyType1> Commented Jan 9, 2013 at 8:55
  • sry, I edited it and question just disappear... Commented Jan 9, 2013 at 9:23

1 Answer 1

2

Add generic type parameters to your Pager variables:

List<MyType1> list1 = new ArrayList<Mytype1>();
List<MyType2> list2 = new ArrayList<Mytype2>();
Pager<MyType1> pager1 = new Pager<MyType1>(
                    page,
                    amount,
                    list1;
                  );
list1 = pager1.getData();


Pager<MyType2> pager2 = new Pager<MyType2>(
                    page,
                    amount,
                    list2
                  );

list2 = pager2.getData();
Sign up to request clarification or add additional context in comments.

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.