I wanted to do a task related to Java collections. Decided to use ArrayList. But I ran into a number of problems:

when added to the collection writes

cannot find symbol method ToyBlocks

And when you try to compare objects through the comparator, you swear at the error:

int cannot be deferenced

Tell me, what are the errors and how to fix it?

import java.util.ArrayList; import java.util.Comparator; class ToyBlocks { String name; int amount; int uah; int kopeck; ToyBlocks(String name, int amount,int uah, int kopeck) { this.name = name; this.amount = amount; this.uah = uah; this.kopeck = kopeck; } } class ToyBlocksComparator implements Comparator { @Override public int compare(Object toyblocks1, Object toyblocks2) { return ((ToyBlocks) toyblocks1).uah.compareTo(((ToyBlocks) toyblocks2).uah);//int cannot be deferenced } } public class SortBlocks { public static void main(String[] args) { ArrayList<ToyBlocks> blocks = new ArrayList(6); blocks.add(ToyBlocks("первый набор", 18, 18, 0));// cannot find symbol method ToyBlocks blocks.add(ToyBlocks("Второй набор", 12, 12, 0)); blocks.trimToSize(); Collections.sort(blocks, new ToyBlocksComparator()); } } 
  • I wrote them in the question - Muscled Boy
  • 2
    You do not have enough new You need blocks.add(new ToyBlocks...... and yet you need to write a sheet: ArrayList<ToyBlocks> blocks = new ArrayList<ToyBlocks>(6); or ArrayList<ToyBlocks> blocks = new ArrayList<>(6); in the 7+ version - Alexey Shimansky
  • And what to do with int can not be deferenced? Am I stating a problem with types? But I wanted to know more specifically - Muscled Boy
  • Because you call the compareTo method on the primitive type int , which is contained in toyblocks1.uah ... and it (the compareTo method) can only be called on reference types ... on objects, krch)) ...... as as a consequence, you need to organize return simple way, i.e. return -1, 0 or 1, comparing the numbers for more less ... or use return Integer.compare(одно число, второе число) - Alexey Shimansky
  • By the way, why do you at compare use Object , and not ToyBlocks as the type of the input parameter? - Alexey Shimansky

1 answer 1

  1. Since the sheet expects to add objects of type ToyBlocks , when calling the add method, you need to write blocks.add(new ToyBlocks...... on the sheet blocks.add(new ToyBlocks...... , i.e. add the keyword new in front of the ToyBlocks , which allows you to create an object of type and place it in the collection.

    A simple call to ToyBlocks(.....) simply means calling the ToyBlocks method. And since this method is not in the class, the error cannot find symbol method ToyBlocks . Actually what it says there: I can not find a method such and such

  2. It’s still worth declaring the collection using generics, i.e. instead

      ArrayList<ToyBlocks> blocks = new ArrayList(6); 

    write

     ArrayList<ToyBlocks> blocks = new ArrayList<ToyBlocks>(6); 

    for java 7+

     ArrayList<ToyBlocks> blocks = new ArrayList<>(6); 
  3. Mistake

    int cannot be deferenced

    in the compare() method arises because you call the compareTo method on the primitive type int , which is contained in toyblocks1.uah , and it (the compareTo method) can only be called on reference types ... on objects, krch))

    If you passed or compared not an int , but an Integer , then it could work.

    But ... as a result, you need to organize return simple way, i.e return -1, 0 or 1 by comparing the numbers for more less. For example:

     public class ToyBlocksComparator implements Comparator<ToyBlocks> { @Override public int compare(ToyBlocks t1, ToyBlocks t2){ return t1.uah - t1.uah; // Ninja method :-) } } 

    It can also be a long way: if (t1.uah < t1.uah) return -1; if (t1.uah == t1.uah) return 0; if (t1.uah > t1.uah) return 1; if (t1.uah < t1.uah) return -1; if (t1.uah == t1.uah) return 0; if (t1.uah > t1.uah) return 1;

    or use the Integer wrapper and call compare already on it.

     return Integer.compare(одно число, второе число); 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky