-1

I am trying to learn programming and started using javafx.

I am now facing a problem which is probably caused by my gaps of knowledge and understanding of java

So far I have created a class as shown below

public class Sheet {

    private String material;
    private int DimX,DimY;
    private double quantity;;

    public Sheet(String _material,int _DimX,int _DimY,double _quantity){
        material = _material;
        DimX =_DimX;
        DimY =_DimY;
        quantity = _quantity ;
    }

    public String getMaterial(){return material;}
    public void setMaterial(String _material) {material = _material;}

    public int getDimX(){return DimX;}
    public void setDimX(int _DimX){DimX = _DimX;}

    public int getDimY(){return DimY;}
    public void setDimY(int _DimY){DimY = _DimY;}

    public double getQuantity(){return quantity;}
    public void setQuantity(double _quantity) {quantity = _quantity;}

}

The arraylist is created using

List<Sheet> Sheet_list= new ArrayList<>();

Through a bit complicated ui the user enters some data which I take parts of, do some calculations and instantiate new entries to an arraylist of the object (sheet)above using the code similar to the linebelow

Sheet_list.add(new Sheet(string_i, int_x, int_y,double_e));

What I need is to sort this arraylist but I am receiving an error that the class is not abstract and does not override abstract method.

Is there any way to sort an arraylist formed from instantiated user data and not programmatically inserted?

Thank you in advance

RAG7
  • 13
  • 7
  • 1
    Where is the code where you create the array list and try to sort it? – lucasvw Apr 26 '17 at 20:49
  • The arraylist is created with List Sheet_list= new ArrayList<>(); and the entries are added in a form like Sheet_list.add(new Sheet(string_i, int_x, int_y,double_e)); – RAG7 Apr 26 '17 at 20:53
  • 1
    please edit your question with the code where you create the list and sort the list – lucasvw Apr 26 '17 at 20:59
  • 2
    As general advice I'd recommend learning programming *then* learning JavaFX. No need to complicate the task of learning programming with also simultaneously trying to learn a complex GUI toolkit. Also, when writing Java, I advise sticking with [Java naming conventions](https://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). – jewelsea Apr 26 '17 at 21:01
  • 1
    likely a duplicate of http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – lucasvw Apr 26 '17 at 21:01
  • I am used at programming simpler programs in C# and little games using Unity but I am trying a bit more advanced (for me) stuff Thank you for the naming conventions link. I will look into it. As far as the duplicate question I hadn't found in my search the above question. I will look into it and then get back to you. Thank you both for the responses – RAG7 Apr 26 '17 at 21:08
  • Possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – user1803551 Apr 27 '17 at 08:20

1 Answers1

0

List interface provides convenient sort() method taking a Comparator. Thus you can either make the Sheet class implement Comparable, then add all your sheet objects to the list, and call Collections.sort(list) on the list object, or use sort(Comparator) version to sort your list.

If you expect that holing your Sheets in some kind of order will be used often, I would go with the first approach.

By the way, I would go with @jewelsea remark and start with basics, then go to JavaFX and more complicated stuff.

ttarczynski
  • 949
  • 1
  • 10
  • 19
  • I have read about comparable and comparator and tried using them but my problem is when I use it on the class above like : public class Sheet implements Comparable I get the "the class is not abstract and does not override abstract method." error. I also tried to cast the array to an observablelist and maybe sort that one but to no avail. The program is almost finished and this is the last hurdle I face. – RAG7 Apr 26 '17 at 21:27
  • The error message you get is clear - if you make your class implement `Comparable` interface, you have to provide an implementation of the compareTo() method - read the documentation, and learn more about the basics of Java and OOP, this will help you understand and solve such problems way more easier. – ttarczynski Apr 26 '17 at 21:31
  • I will work on this in the next few days and when I have a more complete picture of the problem and maybe some new code I will update the question. Thank you all – RAG7 Apr 26 '17 at 21:41