5

I've tried to use Collections.sort(shapes) but it's giving me this error:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the 
 arguments (ArrayList<CreateShape>). The inferred type CreateShape is not a valid substitute for the 
 bounded parameter <T extends Comparable<? super T>>

How should I fix that?

CreateSpace class

 public class CreateSpace implements Space{

            private int height;
        private int width;
        private String layout;
        private char[][] space;
        private Shape originalShape;
        private ArrayList<CreateShape> shapes = new ArrayList<CreateShape>();

        public CreateSpace(int height, int width, char[][] space, String layout)
        {
            this.height = height;
            this.width = width;
            this.space = space;
            this.layout = layout;
        }
    public void placeShapeAt(int row, int col, Shape shape)
        {

            int sHeight = shape.getHeight();
            int sWidth = shape.getWidth();
            if(shape.getHeight() + row > space.length || shape.getWidth() + col > space[0].length)
                throw new FitItException("Out of bounds!");
            char [][] spaceWithShapes = space;
            if(shapeFitsAt(row, col, shape) == true)
            {
                for(int r = 0; r < sHeight; r++)
                {
                    for(int c = 0; c < sWidth; c++)
                    {

                        if(spaceWithShapes[r+row][c+col] == '.' && shape.isFilledAt(r, c) == false)
                            spaceWithShapes[r+row][c+col] = (((CreateShape)shape).getShape()[r][c]);
                    }
                //  shapes.add((CreateShape)shape);
                    Collections.sort(shapes); // <<------- getting an error here
                }
                spaceWithShapes = space;
                shapes.add((CreateShape)shape);
Collections.sort(shapes); // <<------- getting an error here
            }
        }
  • 5
    My first impression is that it's due to your CreateShape class not extending Comparable. You need to @Override a compare() method in order for sorting to work. – CubeJockey Apr 27 '15 at 20:33
  • Check [this](http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/). You need to implement comparable and comparator in your Shape class, and then you can use Collections.sort() on your `ArrayList` – Jonas Czech Apr 27 '15 at 20:34
  • You can extend a Comparator() by overriding compare() method. – BufBills Apr 27 '15 at 20:41
  • 2
    right now you have ```CreateSpace implements Space```, you need to have ```CreateSpace implements Space, Comparable``` – Transcendence Apr 27 '15 at 20:43
  • possible duplicate of [Sort Java Collection](http://stackoverflow.com/questions/6957631/sort-java-collection) – ericbn Apr 27 '15 at 20:53

1 Answers1

2

You got the error because when you call Collections.sort() passing only a List<T> as parameter, it expects that the list elements implement the Comparable interface. Since this is not the case for CreateShape, sort() has no way to know how these objects should be sorted.

Here are two options you should consider:

  1. CreateShape could implement Comparable<CreateShape>: do this if you think CreateShape instances have a natural order in which they should be sorted. If you wanted to sort by a char field, for example:

    class CreateShape implements Comparable<CreateShape> {
         private char displayChar;
    
         public char getDisplayChar() {
             return displayChar; 
         }
    
         @Override
         public int compareTo(CreateShape that) {
             return Character.compare(this.displayChar, that.displayChar);
         }
     }
    

Then you could simply call Collections.sort():

Collections.sort(shapes);
  1. Create a custom Comparator<CreateShape>: do this if you want to sort CreateShape instances arbitrarily. You could have a Comparator that sorts by name, another that sorts by id, etc. Example:

    enum DisplayCharComparator implements Comparator<CreateShape> {
        INSTANCE;
    
        @Override
        public int compare(CreateShape s1, CreateShape s2) {
            return Character.compare(s1.getDisplayChar(), s2.getDisplayChar());
        }
    }
    

Then you should call Collections.sort() passing the comparator as parameter:

Collections.sort(shapes, DisplayCharComparator.INSTANCE);

Note I implemented DisplayCharComparator as a singleton. That's because it has no state, so there is no need to have more than one instance of this comparator. An alternative is to use a static variable:

class CreateShape {

    static final Comparator<CreateShape> DISPLAY_CHAR_COMPARATOR =
        new DisplayCharComparator();

    static class DisplayCharComparator implements Comparator<CreateShape> { ... }

    // rest of the code
}

Or, if you're using Java 8, you can use Comparator.comparing:

shapes.sort(Comparator.comparing(CreateShape::getDisplayChar));
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • Thanks for the reply, @AndersonVieira! But Im trying to compare `char displayChar` (as I do not have any String value) but getting the error when I do this: `return this.dc.compareTo(other.dc);` –  Apr 27 '15 at 20:57
  • @John For comparing `chars`s you could do `return Character.compare(c1, c2);`. I updated the answer. – Anderson Vieira Apr 27 '15 at 21:01