0

I found a class declaration that works for me, but I wonder if this class could be written more "short":

public class Data implements Comparable <Data>{
    public Data     (int category,  String quality, String title ) {
        super();
        this.category = category;
        this.quality = quality;
        this.title = title;
    }
    int    category;
    String quality;
    String title;

    @Override
    public int compareTo(Data d) {
       return this.getDuration_value() - (d.getDuration_value());
    }
}

Why do I have to mention "category, quality an title" this often ? And why "super()" ? I would like to have it just shorter. I found other explanations, but nowhere this "complex structure".

The "data" is given by these lines, and I want that I do not have to declare in advance the length of the array:

Data[] myDataArray = { 
        new Data(0,                 // category "0"
                "***",              // quality
                "MyTitle1")         // title

        new Data(0,                 // same category "0"
                "**",               // quality
                "MyTitle2")         // title
}
OO7
  • 2,719
  • 1
  • 16
  • 30
champagne
  • 3
  • 1
  • 1
    super() invokes the parent class constructor and it is done by default in Java. You can skip this in your example. – Bartek Maraszek Nov 19 '14 at 09:12
  • FYI, questions asking to improve existing working code are better suited for http://codereview.stackexchange.com/ – merours Nov 19 '14 at 09:16
  • Welcome to Stackoverflow. The "repetitions" you are seeing are due to Java's syntax. Each occurrence of `category` has a different meaning. If you do not know these difference I highly advice to learn more about Java - it will help you a lot when coding Java. Also, it is the nature of Arrays that you have to specify their length at Array creation. If you want a more dynamic structure, look at Lists. – SirRichie Nov 19 '14 at 09:18

2 Answers2

1

super() invokes the parent class constructor. If you skip this, Java will invoke the default constructor (with no arguments) automatically, so you can skip this line in your example.

Array's length should be declared in advance. If this is a problem, you should use a container such as ArrayList instead. ArrayList will automatically resize when needed.

As for mentioning the fields multiple times, Java is just this verbose.

Bartek Maraszek
  • 1,274
  • 1
  • 13
  • 30
  • Is there a way to "shorten" the sourcecode? It seems to me pretty long for just defining an Array, but that's why I am not expert. And: Am I right that it is the declaration of an Array, or is it an arraylist (or something similiar)? – champagne Nov 19 '14 at 12:04
  • @champagne, defining an array is a one-liner - see [this question](http://stackoverflow.com/questions/1200621/declare-array-in-java). [This question](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) explains how to initialize an ArrayList. Java is widely known to have a verbose syntax. I'm afraid you cannot get it shorter than that. Scripting languages are generally more compact. You may want to check out [Groovy](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line), a scripting language similar to Java. – Bartek Maraszek Nov 20 '14 at 13:19
  • And yes, you are right - in your example you are using an array. – Bartek Maraszek Nov 20 '14 at 13:28
0

The example you provide implements Comparable which is only necessary if you want to compare your data objects (e.g. is "Obj A" greater than "Obj B" using a comparator). You do not need the constructor. It only exists to ensure your elements are initialized. Otherwise you may end up with NullPointerExceptions and you can't compare.

This example simply is "safe" and can prevent some kinds of problems.

If you just need a "list" you can use ArrayList<Data> or other List options.

Jim
  • 9,902
  • 1
  • 23
  • 35