DevRight
DevRight

Reputation: 395

Make a sub list of a list, which is in an object stored List

So I have an object Called Event which has the variables.

private String id;
private int x;
private int y;
private int distance;
private ArrayList<Double> list;

The list variable holds a list of doubles which represent a list of ticket prices for each event.

I have already sorted the list from Lowest to Highest, so the lowest price is at the top of the list. What I want to do is create a sublist, of 1, which will be the lowest price for each event.

I currently have a list of Events, for which I was able to create a sublist of 5.

 List<Event>resList = new ArrayList<>(unique1.subList(unique1.size() - 5, unique1.size()));

When I try to create a sublist of the List of doubles it will not do it. It gives me no errors but just wont create the list and returns the list unchanged. This is what I tried.

public List<Event> finalTickets(List<Event> list){


        for(int i =0; i < list.size();i++){
            ArrayList<Double> onePrice = new ArrayList<Double>(list.get(i).getList().
                    subList(list.get(i).getList().size()-1, list.get(i).getList().size()));
        }

        return list;
    }

Any help would be great.

Upvotes: 2

Views: 1600

Answers (1)

Eran
Eran

Reputation: 393831

You are not changing the Event instances of the list you pass to the method, and the ArrayList<Double> instances you create inside the method are not referenced by any object.

In order to change the lists of these Events you should modify the Event instances:

for(int i = 0; i < list.size(); i++){
    ArrayList<Double> onePrice = new ArrayList<Double>(list.get(i).getList().
            subList(list.get(i).getList().size()-1, list.get(i).getList().size()));
    list.get(i).setList(onePrice);
}

Upvotes: 1

Related Questions