-6
import java.util.ArrayList;

public class ArrivalsList extends ArrayList<Integer> {

    public int first() {
        return this.get(0);
    }

    public int last() {
        return this.get(this.size() - 1);
    }

}

Is this a good way of creating a custom list? Or is there any other, better way to do this?

Serenity
  • 35,289
  • 20
  • 120
  • 115
Programmer
  • 134
  • 1
  • 12
  • Use the provided one: see https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html#getFirst-- –  Jul 02 '16 at 10:16
  • 2
    No. Extending from ArrayList is not a good idea, and you should [program on interfaces rather than concrete implementations](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). Write your static utility method to get the first / last elements of any list if you find using get() is so difficult. Or create your own class **using** a list, rather than extending it. – JB Nizet Jul 02 '16 at 10:16
  • List and Custom List could confused so ask question properly – Vickyexpert Jul 02 '16 at 10:19
  • Re-Inventing the wheel is the bad idea.... – ΦXocę 웃 Пepeúpa ツ Jul 02 '16 at 11:39
  • In general: When you derive from a class and (only) add public methods to the derivative, it is most likely not a good idea. There are exceptions of course, e.g applying the `LayerSuperType`pattern. – Andreas Mueller Jul 02 '16 at 11:44

1 Answers1

0

I don't understand as well what do you mean by "custom list".

But in your code, it seems you want to retrieve only the first and the last integer from the List. If you want to perform only this two operations, a Queue would be better than an ArrayList in term of performance.

Obviously, if you have to deal with a limited number of data, the solutions are practically equivalent.

Simone C.
  • 369
  • 4
  • 14
  • I will use it for more operations. I just wondered if this is a good way of doing it. I am developing an app for retrieving bus arrivals in my town and i wanted it to get the first and last arrival for now. Thank you :) – Programmer Jul 02 '16 at 10:19