specify array list type from an attribute

Ahmed :

imagine the i have three classes

Feedback
Customer
Supplier

now i have a pagination code that it's only for feedback class

    public ArrayList<Feedback> generatePage(int currentPage,Class paginator_type)
    {
        int startItem=currentPage*ITEMS_PER_PAGE;
        int numOfData=ITEMS_PER_PAGE;
        ArrayList<Feedback> pageData=new ArrayList<>();

        if (currentPage == LAST_PAGE )
        {
            for(int i=startItem;i<startItem+ITEMS_REMAINING;i++){
                String ss[] = all_data[i].split(" ");
                pageData.add(new Feedback(ss[5],ss[3],ss[4],ss[0]));

            }
        }
        else
        {
            for (int i=startItem;i<startItem+numOfData;i++)
            {
            String ss[] = all_data[i].split(" ");
            pageData.add(new Feedback(ss[5],ss[3],ss[4],ss[0]));

            }

        }
        return pageData;
    }

now i need the arraylist type to change depanding of what type of objects iam adding to it i need to make only one method that accepts any type of array list , how can i do it ? and how i can make a parameter that accepts a class type for example :

call method :

makeArrayList(1,Reservation);

method :

public void makeArrayList(int i , Class class) <- how i can declare the parameter
ArrayList<class> aa = new ArrayList<>();
am0awad :

You can define an interface and implement in the classes that you want or abstract class and extend it.

example

public interface IBase { }

public class Feedback implements IBase {}
public class Customer implements IBase {}
public class Supplier implements IBase {}

and in this method change class to be:

public void makeArrayList(int i , IBase parent)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=352287&siteId=1