1

This is my first time to use a List. I have a Class named Foods and I want to make a dynamic list out of it. I wrote the first line that you can see below and then there are a lot of methods that just popped out that I need to override. What should I write in each method? Or is this the right way to make an instance of a list?

public List<Foods> food = new List<Foods>() {

    //then these list of methods just popped out//

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @NonNull
    @Override
    public Iterator<Foods> iterator() {
        return null;
    }

    @NonNull
    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @NonNull
    @Override
    public <T> T[] toArray(@NonNull T[] ts) {
        return null;
    }

    @Override
    public boolean add(Foods foods) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(@NonNull Collection<?> collection) {
        return false;
    }

    @Override
    public boolean addAll(@NonNull Collection<? extends Foods> collection) {
        return false;
    }

    @Override
    public boolean addAll(int i, @NonNull Collection<? extends Foods> collection) {
        return false;
    }

    @Override
    public boolean removeAll(@NonNull Collection<?> collection) {
        return false;
    }

    @Override
    public boolean retainAll(@NonNull Collection<?> collection) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public Foods get(int i) {
        return null;
    }

    @Override
    public Foods set(int i, Foods foods) {
        return null;
    }

    @Override
    public void add(int i, Foods foods) {

    }

    @Override
    public Foods remove(int i) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @NonNull
    @Override
    public ListIterator<Foods> listIterator() {
        return null;
    }

    @NonNull
    @Override
    public ListIterator<Foods> listIterator(int i) {
        return null;
    }

    @NonNull
    @Override
    public List<Foods> subList(int i, int i1) {
        return null;
    }
};
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49

5 Answers5

2

List is an Interface, if you use new directly, you have to implement it as an anonymous class(above code).

Instead, you can use the implementions java.util package provided, such as ArrayList and LinkedList, you can initiate it like this:

public List<Foods> food = new ArrayList<>():

or

public List<Foods> food = new LinkedList<>():
xingbin
  • 27,410
  • 9
  • 53
  • 103
2

List<> is the interface that other list types such ArrayList and LinkedList implement.

When you initialise your List, you want to use one of these other List types that already have the methods that popped up for you implemented.

For example:

public List<Foods> food = new ArrayList<>()
Chris
  • 64
  • 1
  • 4
1

You probably don't need to override any methods, just create a List like this:

private List<Foods> list = new ArrayList<>();

List is an interface, it cannot be instantiated (none of its methods are implemented). ArrayList is one implementation of the List interface, it uses an internal array to store its elements.

By writing List<Foods>, you are declaring that this List will hold elements of type Foods.

You generally should not make fields public, use private and if you need to access them from another class, add a getter/setter method.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • You should not override the methods ideally, `Foods` being a pojo kind of class, it should have all its members private and should have `accessors` and `mutators` for their access from other class. `List list = new ArrayList<>();` is one of the ways to create a new `List` and then you can use `add();` method to add the instance of `Food` in the `list`. – Nisarg Patil Mar 18 '18 at 17:24
1

List is an interface, so if you call it directly, of course you have to override its methods. The proper way to instantiate a List is to instantiate one of the classes which implement it, the most commonly used are ArrayList and LinkedList:

List<Foods> food = new LinkedList<>(); or List<Foods> food = new ArrayList<>(); The first is an ordered list, the second has random access.

I suggest you to read the javadoc of List interface (and of any java object you are not familiar with): you can find useful information on the object you are using, such as if it is a class or an interface and in the last case there is a section called "All Known Implementing Classes" where you can find all the classes that implement it, so you can decide which is the implementation you need.

Spock
  • 315
  • 2
  • 13
0

If there is anything different from ArrayList or LinkedList you would like to implement (something that is special for your food list), then yes, you have to specify these methods. Otherwise, as suggested in previous posts, use this approach:

public List<Foods> food = new ArrayList<>();

Patrick Na
  • 119
  • 1
  • 9
  • nothing special actually. thanks to everyone who answered, i know i'm not supposed to say it (according to the rules). – user9512695 Mar 20 '18 at 12:33